iOS中的多線程方案分為4種瘟裸,從最早的pthread到基于GCD的NSOperation.
1、pthread:(C語(yǔ)言剧辐,非常古老的一種多線程方法)開發(fā)中用的比較少
2带欢、NSThread (OC語(yǔ)言,開發(fā)中用的也比較少镰吆,主要用于調(diào)試程序)
3帘撰、GCD (C語(yǔ)言,蘋果進(jìn)行了封裝万皿,開發(fā)中用的比較多)
4摧找、NSOperation (基于GCD,開發(fā)中用的非常多)
其中3和4系統(tǒng)會(huì)自動(dòng)進(jìn)行內(nèi)存管理。
一牢硅、pthread
pthread是一套純用C語(yǔ)言的API蹬耘,需要程序員自己管理生命周期,基本很少使用减余。
需要導(dǎo)入頭文件#import<pthread/phtread.h>
<pre>
// 耗時(shí)操作
void * execFunc (void *param) {
NSString *result = (__bridge NSString *)param;
NSLog(@"%@===>%@",[NSThread currentThread],result);
for (int i=0; i<100* 100; i++) {
NSLog(@"%d",i);
}
return NULL;
}
//1综苔、創(chuàng)建一條線程
pthread_t pthreadId;//線程Id
NSString * param = @"Hello, world";
int result = pthread_create(&pthreadId, NULL, execFunc, (__bridge void*)(param));
if (result == 0) {
NSLog(@"線程創(chuàng)建成功");
}else{
NSLog(@"線程創(chuàng)建失敗");
}
</pre>
二、NSThread
NSThread是基于線程使用位岔,輕量級(jí)的多線程編程方法(相對(duì)GCD和NSOperation)如筛,一個(gè)NSThread對(duì)象代表一個(gè)線程,需要手動(dòng)管理線程的生命周期抒抬,處理線程同步等問題杨刨。
2.1、NSThread線程創(chuàng)建
NSThread線程創(chuàng)建的三種方法
1擦剑、對(duì)象方法
<pre>
-(void)dynamicCreateThread
{
// 創(chuàng)建線程對(duì)象
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageSource:) object:self.imageUrl];
// 線程開啟
[thread start];
}</pre>
2妖胀、創(chuàng)建線程后自動(dòng)啟動(dòng)線程
<code>[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];</code>
3、隱式創(chuàng)建并啟動(dòng)線程
<code>[self performSelectorInBackground:@selector(run) withObject:nil]; </code>
3.1惠勒、結(jié)束當(dāng)前所在線程回歸主線程赚抡,并在主線程執(zhí)行方法,使用Object傳值纠屋,完成線程間通信涂臣。
<code>[self performSelectorOnMainThread:@selector(refreshImageView:) withObject:image waitUntilDone:YES];</code>
2.2、線程狀態(tài)
程序中的線程可能的狀態(tài)演變
線程狀態(tài)的控制方法
1售担、啟動(dòng)線程
<code>- (void)start; </code>// 進(jìn)入就緒狀態(tài) -> 運(yùn)行狀態(tài)赁遗。當(dāng)線程任務(wù)執(zhí)行完畢闯估,自動(dòng)進(jìn)入死亡狀態(tài)
2、阻塞(暫停)線程
<code>+ (void)sleepUntilDate:(NSDate *)date;</code>
<code>+ (void)sleepForTimeInterval:(NSTimeInterval)time;</code>
3吼和、強(qiáng)制停止線程
<code>+ (void)exit;</code>// 進(jìn)入死亡狀態(tài)
注意:**一旦線程停止(死亡)了,就不能再次開啟任務(wù)
2.3 NSThread注意事項(xiàng)
<code>- (void) cancel;</code>
1骑素、當(dāng)我們需要中途停止線程時(shí)炫乓,我們不應(yīng)該調(diào)用exit方法,而是調(diào)用cancel方法献丑。因?yàn)槟┑罚绻覀冎苯诱{(diào)用
exit方法的話,線程是直接退出创橄,而沒有機(jī)會(huì)去執(zhí)行清理操作箩做,可能會(huì)產(chǎn)生內(nèi)存泄漏!
<pre>// 停止線程
- (void) stopThread{
NSLog(@"Cancelling the Thread");
[self.myThread cancel];
NSLog(@"Releasing the thread");
self.myThread = nil;
}</pre>
2妥畏、我們必須要清楚這么一個(gè)現(xiàn)象邦邦!
當(dāng)線程在執(zhí)行過程中,如果被sleepForTimeInterval后醉蚁,線程將會(huì)被進(jìn)入休眠燃辖。那么在它休眠期間又被cancel后,那么网棍,事實(shí)上黔龟,線程在醒來后,任然會(huì)執(zhí)行完它的操作滥玷。
NSThread其余常用操作
主線程相關(guān)方法
<code>+ (NSThread *)mainThread; // 獲得主線程</code>
<code>- (BOOL)isMainThread; // 是否為主線程</code>
<code>+ (BOOL)isMainThread; // 是否為主線程</code>
獲得當(dāng)前線程
<code>NSThread *current = [NSThread currentThread];</code>
線程的調(diào)度優(yōu)先級(jí)
<code>+ (double)threadPriority;</code>
<code>+ (BOOL)setThreadPriority:(double)p;</code>
<code>- (double)threadPriority;</code>
<code>- (BOOL)setThreadPriority:(double)p;</code>
復(fù)制代碼
調(diào)度優(yōu)先級(jí)的取值范圍是0.0 ~ 1.0氏身,默認(rèn)0.5,值越大惑畴,優(yōu)先級(jí)越高