多線程編程1-NSThread


前言

每個iOS應用程序都有個專門用來更新顯示UI界面、處理用戶觸摸事件的主線程,因此不能將其他太耗時的操作放在主線程中執行,不然會造成主線程堵塞(出現卡機現象),帶來極壞的用戶體驗。一般的解決方案就是將那些耗時的操作放到另外一個線程中去執行,多線程編程是防止主線程堵塞,增加運行效率的最佳方法。

iOS中有3種常見的多線程編程方法:

1.NSThread

這種方法需要管理線程的生命周期、同步、加鎖問題,會導致一定的性能開銷

 

2.NSOperation和NSOperationQueue

是基於OC實現的。NSOperation以面向對象的方式封裝了需要執行的操作,然后可以將這個操作放到一個NSOperationQueue中去異步執行。不必關心線程管理、同步等問題。

 

3.Grand Centeral Dispatch

簡稱GCD,iOS4才開始支持,是純C語言的API。自iPad2開始,蘋果設備開始有了雙核CPU,為了充分利用這2個核,GCD提供了一些新特性來支持多核並行編程

 

這篇文章簡單介紹NSThread這個類,一個NSThread實例就代表着一條線程

一、獲取當前線程

NSThread *current = [NSThread currentThread];

 

二、獲取主線程

1 NSThread *main = [NSThread mainThread];
2 NSLog(@"主線程:%@", main);

打印結果是:

2013-04-18 21:36:38.599 thread[7499:c07] 主線程:<NSThread: 0x71434e0>{name = (null), num = 1}

num相當於線程的id,主線程的num是為1的

 

三、NSThread的創建

1.動態方法

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; 

* 在第2行創建了一條新線程,然后在第4行調用start方法啟動線程,線程啟動后會調用self的run:方法,並且將@"mj"作為方法參數

1 // 初始化線程
2 NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"mj"] autorelease];
3 // 開啟線程
4 [thread start];

假如run:方法是這樣的:

1 - (void)run:(NSString *)string {
2     NSThread *current = [NSThread currentThread];
3     NSLog(@"執行了run:方法-參數:%@,當前線程:%@", string, current);
4 }

打印結果為:

2013-04-18 21:40:33.102 thread[7542:3e13] 執行了run:方法-參數:mj,當前線程:<NSThread: 0x889e8d0>{name = (null), num = 3}

可以發現,這條線程的num值為3,說明不是主線程,主線程的num為1

 

2.靜態方法

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"mj"];

執行完上面代碼后會馬上啟動一條新線程,並且在這條線程上調用self的run:方法,以@"mj"為方法參數

 

3.隱式創建線程

[self performSelectorInBackground:@selector(run:) withObject:@"mj"]; 

會隱式地創建一條新線程,並且在這條線程上調用self的run:方法,以@"mj"為方法參數

 

四、暫停當前線程

[NSThread sleepForTimeInterval:2];
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];  
[NSThread sleepUntilDate:date];

上面兩種做法都是暫停當前線程2秒

 

五、線程的其他操作

1.在指定線程上執行操作

1 [self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES]; 

* 上面代碼的意思是在thread這條線程上調用self的run方法

* 最后的YES代表:上面的代碼會阻塞,等run方法在thread線程執行完畢后,上面的代碼才會通過

 

2.在主線程上執行操作

[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];  

在主線程調用self的run方法

 

3.在當前線程執行操作

[self performSelector:@selector(run) withObject:nil]; 

在當前線程調用self的run方法

 

六、優缺點

1.優點:NSThread比其他多線程方案較輕量級,更直觀地控制線程對象

2.缺點:需要自己管理線程的生命周期,線程同步。線程同步對數據的加鎖會有一定的系統開銷


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM