iOS開(kāi)發(fā)中不能將太耗時(shí)的操作放在主線程中執(zhí)行,否則會(huì)造成線程的阻塞。通常解決方案就是將耗時(shí)的操作另開(kāi)一個(gè)線程執(zhí)行。

iOS 支持多個(gè)層次的多線程編程,層次越高的抽象程度越高,使用起來(lái)也越方便,也是蘋(píng)果最推薦使用的方法。下面根據(jù)抽象層次從低到高依次列出iOS所支持的多線程編程范式:
- Thread :是三種方法里面相對(duì)輕量級(jí)的,但需要管理線程的生命周期、同步、加鎖問(wèn)題,這會(huì)導(dǎo)致一定的性能開(kāi)銷。
-
Cocoa Operations:是基于OC實(shí)現(xiàn)的,NSOperation以面向?qū)ο蟮姆绞椒庋b了需要執(zhí)行的操作,不必關(guān)心線程管理、同步等問(wèn)題。NSOperation是一個(gè)抽象基類,iOS提供了兩種默認(rèn)實(shí)現(xiàn):
NSInvocationOperation和NSBlockOperation,當(dāng)然也可以自定義NSOperation。 - GCD(iOS4才開(kāi)始支持):提供了一些新特性、運(yùn)行庫(kù)來(lái)支持多核并行編程,它的關(guān)注點(diǎn)更高:如何在多個(gè)cpu上提升效率。
一、創(chuàng)建NSthread
NSThread的創(chuàng)建主要有3種直接方式:
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// 調(diào)用完畢后,會(huì)馬上創(chuàng)建并開(kāi)啟新線程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start]; // 開(kāi)啟線程
[self performSelectorInBackground:@selector(run) withObject:nil];
第一種會(huì)立即創(chuàng)建一個(gè)線程來(lái)做事情;第二種雖然你 alloc 了也init了,但是要直到我們手動(dòng)調(diào)用start 啟動(dòng)線程時(shí)才會(huì)真正去創(chuàng)建線程。這種延遲實(shí)現(xiàn)思想在很多跟資源相關(guān)的地方都有用到,還可以在啟動(dòng)線程之前,對(duì)線程進(jìn)行配置,比如設(shè)置stack 大小,線程優(yōu)先級(jí)。第三種間接的方式,更加方便,我們甚至不需要顯式編寫(xiě) NSThread 相關(guān)代碼。
二、NSthread常用方法:
NSThread *current = [NSThread currentThread]; //獲取當(dāng)前線程
NSThread *main = [NSThread mainThread]; //獲取當(dāng)前主線程
[NSThread sleepForTimeInterval:2]; // 暫停2s
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];
[NSThread sleepUntilDate:date];// 暫停2s
三、線程間通信
//主線程上執(zhí)行操作:
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
//當(dāng)前線程上執(zhí)行操作:
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
//指定線程上執(zhí)行操作:
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
所謂線程間通信,即如何從一個(gè)線程進(jìn)入到另一個(gè)線程繼續(xù)執(zhí)行任務(wù)或者是傳遞參數(shù)(如從子線程回到主線程)。