iOS多线程技术
iOS多線程技術(shù)
iOS有三種多線程編程技術(shù):
- NSThread
- NSOperation
- GCD
它們的抽象程度由低到高,越高的使用起來越簡單。
NSThread
顯示調(diào)用NSThread類
類方法
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"hi"];實(shí)例方法
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(doSomething:) object:@"hi"]; [thread start];
隱式調(diào)用
開啟后臺線程
[self performSelectorInBackground:@selector(doSomething:) withObject:@"hi"];在主線程中運(yùn)行
[self performSelectorOnMainThread:@selector(doSomething:) withObject:@"hi" waitUntilDone:YES];在指定線程中執(zhí)行,但該線程必須具備run loop
[self performSelector::@selector(doSomething:) onThread:thread withObject:@"hi" waitUntilDone:YES];
常見NSThread的方法
+ (NSThread *)currentThread; //獲得當(dāng)前線程 + (void)sleepForTimeInterval:(NSTimeInterval)ti; //線程休眠 + (NSThread *)mainThread; //主線程,亦即UI線程了 - (BOOL)isMainThread; + (BOOL)isMainThread; //當(dāng)前線程是否主線程 - (BOOL)isExecuting; //線程是否正在運(yùn)行 - (BOOL)isFinished; //線程是否已結(jié)束NSOperation
- NSInvocationOperation
- NSBlockOperation
“`
總結(jié)
- 上一篇: iOS开发之多线程
- 下一篇: iOS中实现多线程的技术方案