LHF Objective-C语法(12)多线程
1、NSThread
(1)创建线程对象的方法:
a、-(id) init;
b、-(id) initWithTarget:(id) target selector:(SEL) selector object:(id) argument;
c、+(void) detachNewThreadSelector:(SEL) aSelector toTarget:(id) aTarget withObject:(id) anArgument //使用这个类方法,不用release(因为没有使用alloc)
启动一个线程用star,结束一个线程用exit(使用exit时,首先要将这个线程的引用计数归0)
(2)NSCondition 执行同步操作,相当于java的lock
(3)@synchronized(要加锁的对象){
//同步执行的代码
}
例1:
(4)与主线程交互
如果想更新UI上的某一个部件,就需要在发起的新线程里调用UI所在的主线程上的一个方法,新线程不能直接访问主线程的方法,需要在润方法中使用如下方法
[self performSelectorOnMainThread:@selector(moveText) withObject:nil waitUntilDone:NO];
(3)线程池