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

來源:互聯網
上載者:User

標籤:strong   資料   os   art   問題   for   

1.NSThread

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

2.NSOperation和NSOperationQueue

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

3.Grand Centeral Dispatch

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

 一個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.缺點:需要自己管理線程的生命週期,線程同步。線程同步對資料的加鎖會有一定的系統開銷

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.