iOS實現(xiàn)多線程編程有四種方式Pthreads,NSThread,NSOperation & NSOperationQueue和GCD.GCD(Grand Central Dispatch)是異步執(zhí)行任務(wù)的技術(shù)之一,一般將應(yīng)用程序中線程管理代碼在系統(tǒng)級實現(xiàn).開發(fā)過程中只需要定義想執(zhí)行的任務(wù)并追加到適當(dāng)?shù)腄ispatch Queue,GCD就能生成必要的線程執(zhí)行任務(wù).
GCD以其難以置信的API風(fēng)格,實現(xiàn)了極其復(fù)雜繁瑣的多線程編程,是多線程編程中必不可少一把利劍.GCD屬于系統(tǒng)級的線程管理,GCD中的FIFO隊列稱為dispatch queue,用來保證先進來的任務(wù)先得到執(zhí)行.
基礎(chǔ)概念
進程是一個正在執(zhí)行程序的實例,在iOS中可以理解為一個正在運行App,每個進程都有一個單獨的Process ID(進程ID).現(xiàn)在操作系統(tǒng)將線程作為基本的操作單元,線程是一組寄存器的狀態(tài),一個進程包含了多個線程.一個進程內(nèi)的所有線程都共享虛擬內(nèi)存空間,文件描述符和各種句柄.單核CPU每次執(zhí)行的CPU命令數(shù)為1,那么單核CPU是如何做到同時執(zhí)行多條命令呢?
OS X和iOS的核心XNU內(nèi)存在發(fā)生操作系統(tǒng)事件時(如每隔一定時間,喚起系統(tǒng)調(diào)用等)會切換執(zhí)行路徑.執(zhí)行路徑中的狀態(tài),例如CPU的寄存器狀態(tài)等信息會保存到各自專用的內(nèi)存塊中,從切換目標路徑到專用的內(nèi)存塊中,復(fù)原CPU寄存器信息,繼續(xù)執(zhí)行切換路徑的CPU命令列.稱之為"上下文切換".
由于使用多線程的程序可以在在某個線程和其他線程之間反復(fù)多次進行上下文切換,因此單核CPU看起來像并列執(zhí)行多個線程一樣,如果是多核CPU,是真正的提供了多個CPU的執(zhí)行多個線程的技術(shù).
多線程編程中最容易出現(xiàn)的問題是多個線程更新相同的資源導(dǎo)致數(shù)據(jù)不一致(數(shù)據(jù)競爭),停止等待事件會導(dǎo)致多個線程相互持續(xù)等待(死鎖),使用太多的線程會消耗大量的內(nèi)存.多線程能夠保證應(yīng)用程序的響應(yīng)性能.
基礎(chǔ)知識
GCD編程中只需要將執(zhí)行的任務(wù)加入Dispatch Queue中即可,Dispatch Queue按照添加的順序FIFO(First-In-First-Out)先入先出的規(guī)則執(zhí)行處理.Dispatch Queue有兩種類型串行隊列(Serial Dispatch Queue)和并行隊列(Concurrent Dispatch Queue) .
串行隊列使用一個線程執(zhí)行任務(wù),等待上一個任務(wù)執(zhí)行完成之后執(zhí)行下一個任務(wù).并行隊列會使用多個線程執(zhí)行任務(wù),不需要等待之前的任務(wù)是否完成.iOS 和 OS X的核心-XNU內(nèi)核決定應(yīng)當(dāng)使用的線程數(shù),生成需要的線程來執(zhí)行處理,當(dāng)處理結(jié)束,應(yīng)當(dāng)執(zhí)行的處理數(shù)減少時,XNU內(nèi)核會結(jié)束不在需要的線程.
GCD中dispatch_get_main_queue獲得主線程中執(zhí)行的隊列,因為主線程只有一個,所以主隊列是串行列.
dispatch_get_global_queue是所有應(yīng)用程序都能創(chuàng)建的并發(fā)隊列,不需要手動創(chuàng)建Dispatch隊列.
GCD 有兩種派發(fā)方式:同步派發(fā)和異步派發(fā),這里的同步和異步指的是 “任務(wù)派發(fā)方式”,而非任務(wù)的執(zhí)行方式.
dispatch_sync定義如下:
/*!
* @function dispatch_sync
*
* @abstract
* Submits a block for synchronous execution on a dispatch queue.
*
* @discussion
* Submits a block to a dispatch queue like dispatch_async(), however
* dispatch_sync() will not return until the block has finished.
*
* Calls to dispatch_sync() targeting the current queue will result
* in dead-lock. Use of dispatch_sync() is also subject to the same
* multi-party dead-lock problems that may result from the use of a mutex.
* Use of dispatch_async() is preferred.
*
* Unlike dispatch_async(), no retain is performed on the target queue. Because
* calls to this function are synchronous, the dispatch_sync() "borrows" the
* reference of the caller.
*
* As an optimization, dispatch_sync() invokes the block on the current
* thread when possible.
*
* @param queue
* The target dispatch queue to which the block is submitted.
* The result of passing NULL in this parameter is undefined.
*
* @param block
* The block to be invoked on the target dispatch queue.
* The result of passing NULL in this parameter is undefined.
*/
#ifdef __BLOCKS__
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);
#endif
- 將block的中的任務(wù)同步提交給目標Queue執(zhí)行.
- 阻塞當(dāng)前Queue,執(zhí)行block任務(wù),回調(diào)當(dāng)前Queue.
這就很容易解釋設(shè)置dispatch_sync的隊列為當(dāng)前Queue發(fā)生死鎖的原因,調(diào)用dispatch_sync會阻塞主隊列,block無法執(zhí)行,不會回調(diào)主隊列.下面的代碼是無法運行的:
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"FlyElephant---執(zhí)行了");
});
dispatch_async與dispatch_sync最大不同的是不會阻塞當(dāng)前隊列:
- 將block的中的任務(wù)同步提交給目標Queue執(zhí)行.
- 不阻塞當(dāng)前Queue,執(zhí)行block任務(wù),回調(diào)當(dāng)前Queue.
dispatch_async(dispatch_get_main_queue(), ^{
for (NSInteger i=0; i < 20; i++) {
NSLog(@"FlyLElephant---異步:%ld",i);
}
});
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"同步數(shù)據(jù)");
});
并行與串行(主線程是串行隊列,dispatch_get_global_queue獲取對全局隊列是并行隊列):
dispatch_queue_t serialQueue = dispatch_queue_create("com.flyelephant.wwww", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.flyelephant.www", DISPATCH_QUEUE_CONCURRENT);
全局隊列有四個優(yōu)先級High,Default,Low和Background:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
串行和并行執(zhí)行順序:
dispatch_queue_t serialQueue = dispatch_queue_create("com.flyelephant.wwww", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
NSLog(@"Serial--1");
});
dispatch_async(serialQueue, ^{
NSLog(@"Serial--2");
});
dispatch_async(serialQueue, ^{
NSLog(@"Serial--3");
});
dispatch_async(serialQueue, ^{
NSLog(@"Serial--4");
});
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.flyelephant.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
NSLog(@"Concurrent--1");
});
dispatch_async(concurrentQueue, ^{
NSLog(@"Concurrent--2");
});
dispatch_async(concurrentQueue, ^{
NSLog(@"Concurrent--3");
});
dispatch_async(concurrentQueue, ^{
NSLog(@"Concurrent--4");
});
dispatch_set_target_queue
dispatch_set_target_queue除了能用來設(shè)置隊列的優(yōu)先級之外,還能夠創(chuàng)建隊列的層次體系,將不同異步任務(wù)設(shè)置為目標隊列.
dispatch_queue_t targetQueue = dispatch_queue_create("targetQueue", DISPATCH_QUEUE_SERIAL);//目標隊列
dispatch_queue_t serialQueue = dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);//串行隊列
dispatch_queue_t conQueue = dispatch_queue_create("queue1", DISPATCH_QUEUE_CONCURRENT);//并發(fā)隊列
dispatch_set_target_queue(serialQueue, targetQueue);
dispatch_set_target_queue(conQueue, targetQueue);
dispatch_async(serialQueue, ^{
NSLog(@"FlyElephant---任務(wù)1 開始");
[NSThread sleepForTimeInterval:3.f];
NSLog(@"FlyElephant---任務(wù)1 結(jié)束");
});
dispatch_async(conQueue, ^{
NSLog(@"FlyElephant---任務(wù)2 開始");
[NSThread sleepForTimeInterval:2.f];
NSLog(@"FlyElephant---任務(wù)2 結(jié)束");
});
dispatch_async(conQueue, ^{
NSLog(@"FlyElephant---任務(wù)3 開始");
[NSThread sleepForTimeInterval:1.f];
NSLog(@"FlyElephant---任務(wù)3 結(jié)束");
});
dispatch_after延遲執(zhí)行
dispatch_after只是延時提交block,不是延時立刻執(zhí)行.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"延遲執(zhí)行");
});
dispatch_barrier_async
dispatch_barrier_async函數(shù)類似于多線程中國柵欄的作用,它等待所有位于barrier函數(shù)之前的操作執(zhí)行完畢后執(zhí)行,并且在barrier函數(shù)執(zhí)行之后,barrier函數(shù)之后的操作才會得到執(zhí)行,該函數(shù)需要同dispatch_queue_create函數(shù)生成的concurrent Dispatch Queue隊列一起使用.
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.barrier.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-1");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-2");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-3");
});
dispatch_barrier_async(concurrentQueue, ^(){
NSLog(@"FlyElephant---dispatch_barrier_async");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-4");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-5");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"FlyElephant-6");
});
dispatch_apply
dispatch_apply類似一個for循環(huán),會在指定的dispatch queue中運行block任務(wù)n次,如果隊列是并發(fā)隊列,則會并發(fā)執(zhí)行block任務(wù),dispatch_apply是一個同步調(diào)用,block任務(wù)執(zhí)行n次后才返回.dispatch_apply會將Block任務(wù)執(zhí)行完成之后才會繼續(xù)往下執(zhí)行.
dispatch_queue_t queue = dispatch_queue_create("com.flyelephant.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(10, queue, ^(size_t i) {
NSLog(@"并行執(zhí)行---%ld",i);
});
NSLog(@"FlyElephant---執(zhí)行完成");
dispatch_queue_t serialQueue = dispatch_queue_create("com.flyelephant.www", DISPATCH_QUEUE_SERIAL);
dispatch_apply(10, serialQueue, ^(size_t i) {
NSLog(@"串行執(zhí)行---%ld",i);
});
NSLog(@"FlyElephant---執(zhí)行完成");
dispatch_once
dispatch_once只執(zhí)行一次,單例實現(xiàn)中使用dispatch_once比較常見.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
dispatch_group_t
dispatch_group_t創(chuàng)建一個隊列組,dispatch_group_wait表示等待group設(shè)置的時間,dispatch_group_notify是在所有的queue任務(wù)完成之后執(zhí)行notify里面的代碼.
dispatch_queue_t dispatchQueue = dispatch_queue_create("com.group.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"FlyElephant---任務(wù)1完成");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
sleep(3);
NSLog(@"FlyElephant---任務(wù)2完成");
});
dispatch_group_notify(dispatchGroup, dispatchQueue, ^{
NSLog(@"dispatch_group_notify 執(zhí)行");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
dispatch_group_wait(dispatchGroup, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
NSLog(@"dispatch_group_wait 結(jié)束");
});
可以將wait的時間設(shè)置一直等待,dispatch_group_wait會阻塞當(dāng)前線程(所以不能放在主線程調(diào)用)一直等待,當(dāng)group上任務(wù)完成,或者等待時間超過設(shè)置的超時時間會結(jié)束等待.
dispatch_queue_t dispatchQueue = dispatch_queue_create("com.group.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"FlyElephant---任務(wù)1完成");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
sleep(3);
NSLog(@"FlyElephant---任務(wù)2完成");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"FlyElephant---任務(wù)3完成");
});
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER);
NSLog(@"dispatch_group_wait 結(jié)束");
dispatch_group_async(dispatchGroup, dispatchQueue, ^{;
NSLog(@"FlyElephant---任務(wù)4完成");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"FlyElephant---任務(wù)5完成");
});
dispatch_group_enter與dispatch_group_leave表示加入任務(wù)組和離開任務(wù)組,兩者要同時出現(xiàn),不然會出現(xiàn)崩潰.
dispatch_queue_t dispatchQueue = dispatch_queue_create("com.group.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_group_enter(dispatchGroup);
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
sleep(2);
NSLog(@"FlyElephant---任務(wù)1完成");
dispatch_group_leave(dispatchGroup);
});
});
dispatch_group_enter(dispatchGroup);
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
sleep(1);
NSLog(@"FlyElephant---任務(wù)2完成");
dispatch_group_leave(dispatchGroup);
});
});
dispatch_group_enter(dispatchGroup);
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
NSLog(@"FlyElephant---任務(wù)3完成");
dispatch_group_leave(dispatchGroup);
});
});
dispatch_group_notify(dispatchGroup, dispatchQueue, ^{
NSLog(@"FlyElephant---dispatch_group_notify任務(wù)完成");
});
所有的任務(wù)執(zhí)行完成之后執(zhí)行通知:
如果不加入enter和leave,看下效果:
dispatch_queue_t dispatchQueue = dispatch_queue_create("com.group.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
sleep(2);
NSLog(@"FlyElephant---任務(wù)1完成");
});
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
sleep(3);
NSLog(@"FlyElephant---任務(wù)2完成");
});
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"FlyElephant---dispatch_group_notify");
});
dispatch_semaphore_t
dispatch_semaphore_t是一種保護線程同步的機制,使用dispatch_semaphore_signal加1,dispatch_semaphore_wait減1,為0的時候等待的設(shè)置方式來達到線程同步的目的和同步鎖一樣能夠解決資源搶占的問題.
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
self.data = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 10; i++) {
dispatch_async(queue, ^(){
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[self.data addObject:[NSString stringWithFormat:@"FlyElephant---%ld",i]];
dispatch_semaphore_signal(semaphore);
});
}