GCD作為日常開發中的使用已經非常普遍,基于C的API為應用在多核硬件上高效運行提供了有力支持。本文分場景寫了幾個測試Demo,方便大家理解與應用。
1,dispatch_get_global_queue與dispatch_get_main_queue交互
很多應用場景需要后臺讀寫大量數據,通過dispatch_get_global_queue函數可以獲取全局隊列來并發執行后臺任務,并再結束后更新UI,保證應用的流程,避免主線程阻塞。代碼如下:
- (void)test1
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 10; i++) {
dispatch_group_async(group, queue, ^{
NSLog(@"___%i %@", i, [NSThread currentThread]);
});
}
dispatch_group_notify(group, queue, ^{
NSLog(@"currentThread1 %@",[NSThread currentThread]);
NSLog(@"執行完畢");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"currentThread2 %@",[NSThread currentThread]);
NSLog(@"main_queue 執行完畢");
});
});
}
執行結果:
test1:
2016-03-10 17:21:37.293 GCDSample[3892:2238505] ___0 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238506] ___2 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238504] ___3 <NSThread: 0x12e689760>{number = 3, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238507] ___1 <NSThread: 0x12e683590>{number = 5, name = (null)}
2016-03-10 17:21:37.294 GCDSample[3892:2238505] ___4 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238506] ___5 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238504] ___6 <NSThread: 0x12e689760>{number = 3, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238507] ___7 <NSThread: 0x12e683590>{number = 5, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238505] ___8 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.298 GCDSample[3892:2238506] ___9 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.299 GCDSample[3892:2238506] currentThread1 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.299 GCDSample[3892:2238506] 執行完畢
2016-03-10 17:21:37.302 GCDSample[3892:2238481] currentThread2 <NSThread: 0x12e50c390>{number = 1, name = main}
2016-03-10 17:21:37.303 GCDSample[3892:2238481] main_queue 執行完畢
2,異步線程中串行執行任務
test1中dispatch_group_async以并發的方式開啟異步線程,不能保證執行順序,如果想在并發線程中串行執行任務該如何做呢?只需要創建一個串行隊列,加入group任務即可。
- (void)test1
{
NSLog(@"code begin");
NSLog(@"currentThread0 %@",[NSThread currentThread]);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t serialQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
for (int i = 0; i < 10; i++) {
dispatch_group_async(group, serialQueue, ^{
[NSThread sleepForTimeInterval:.1f];
NSLog(@"___%i %@", i, [NSThread currentThread]);
});
}
dispatch_group_notify(group, serialQueue, ^{
NSLog(@"currentThread1 %@",[NSThread currentThread]);
NSLog(@"執行完畢");
});
NSLog(@"code end");
}
執行結果:
test2:
2016-03-10 17:07:10.366 GCDSample[3879:2235420] code begin
2016-03-10 17:07:10.367 GCDSample[3879:2235420] currentThread0 <NSThread: 0x14c50c350>{number = 1, name = main}
2016-03-10 17:07:10.367 GCDSample[3879:2235420] code end
2016-03-10 17:07:10.472 GCDSample[3879:2235438] ___0 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.577 GCDSample[3879:2235438] ___1 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.683 GCDSample[3879:2235438] ___2 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.784 GCDSample[3879:2235438] ___3 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.890 GCDSample[3879:2235438] ___4 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.995 GCDSample[3879:2235438] ___5 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.101 GCDSample[3879:2235438] ___6 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.206 GCDSample[3879:2235438] ___7 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.309 GCDSample[3879:2235438] ___8 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] ___9 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] currentThread1 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] 執行完畢
3,dispatch_barrier_async,分割多任務線程
如果有10個并發任務,想要分成兩組,比如指定前五個任務執行完之后,優先執行第六個任務,再執行剩下的操作,可以通過dispatch_barrier_async來分割開,示例:
- (void)test3
{
dispatch_queue_t concurrentQueue = ({
dispatch_queue_t queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
queue;
});
for (int i = 0; i < 10; i++) {
if (i != 5) {
dispatch_async(concurrentQueue, ^{
[NSThread sleepForTimeInterval:arc4random_uniform(3)];
NSLog(@"dispatch_async %i", i);
});
}else{
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"dispatch_barrier_async");
});
}
}
}
執行結果:
test3
2016-03-10 17:47:32.397 GCDSample[3949:2246479] dispatch_async 1
2016-03-10 17:47:32.400 GCDSample[3949:2246481] dispatch_async 3
2016-03-10 17:47:33.403 GCDSample[3949:2246480] dispatch_async 2
2016-03-10 17:47:34.400 GCDSample[3949:2246477] dispatch_async 0
2016-03-10 17:47:34.405 GCDSample[3949:2246479] dispatch_async 4
2016-03-10 17:47:34.406 GCDSample[3949:2246477] dispatch_barrier_async
2016-03-10 17:47:34.406 GCDSample[3949:2246479] dispatch_async 6
2016-03-10 17:47:35.411 GCDSample[3949:2246479] dispatch_async 7
2016-03-10 17:47:35.412 GCDSample[3949:2246480] dispatch_async 9
2016-03-10 17:47:35.411 GCDSample[3949:2246477] dispatch_async 8
4,串行隊列死鎖
- (void)test4
{
NSLog(@"1");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"2");
});
NSLog(@"3");
}
- (void)test4_1 {
NSLog(@"1");
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"2 %@",[NSThread currentThread]);
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"3 %@",[NSThread currentThread]);
});
NSLog(@"4 %@",[NSThread currentThread]);
});
NSLog(@"5");
}
方法test4會造成死鎖,因為main queue需要等待dispatch_sync函數中block返回才能繼續執行,而通過dispatch_sync放入main queue的block按照FIFO的原則(先入先出)現在得不到執行,就造成了相互等待的局面,產生死鎖。將同步的串行隊列放到另外一個異步線程就能夠解決(如方法test4_1所示)。所以在使用dispatch_sync的時候需要很謹慎,需要先執行[NSThread isMainThread]
判斷下當前任務是否在mian queue中調用,比如有一段代碼在后臺執行,而它需要從界面控制層獲取一個值。那么你可以使用dispatch_sync簡單辦到。執行結果:
test4
2016-03-10 17:23:30.020 GCDSample[3896:2239119] 1
test4_1
2016-03-11 17:30:11.900 GCDSample[1197:730552] 1
2016-03-11 17:30:11.901 GCDSample[1197:730552] 5
2016-03-11 17:30:11.903 GCDSample[1197:730564] 2 <NSThread: 0x170263080>{number = 2, name = (null)}
2016-03-11 17:30:11.933 GCDSample[1197:730552] 3 <NSThread: 0x174075400>{number = 1, name = main}
2016-03-11 17:30:11.933 GCDSample[1197:730564] 4 <NSThread: 0x170263080>{number = 2, name = (null)}
5,dispatch_semaphore_t 控制并發線程數
有時在執行多任務時需要避免搶占資源以及性能過多消耗的情況,需要在特定時間內控制同時執行的任務數量,在NSOperationQueue可以通過maxConcurrentOperationCount來控制,在GCD中可以指定semaphore來控制了。先介紹3個函數,dispatch_semaphore_create創建一個semaphore;dispatch_semaphore_wait等待信號,當信號總量少于0的時候就會一直等待,否則就可以正常的執行,并讓信號總量-1;dispatch_semaphore_signal是發送一個信號,自然會讓信號總量加1。看一個小例子:
- (void)test7
{
_semaphore = dispatch_semaphore_create(2);
[self task7_1];
[self task7_2];
[self task7_3];
[self task7_4];
}
- (void)task7_1
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"7_1_begin");
sleep(4);
NSLog(@"7_1_end");
dispatch_semaphore_signal(_semaphore);
});
}
- (void)task7_2
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"7_2_begin");
sleep(4);
NSLog(@"7_2_end");
dispatch_semaphore_signal(_semaphore);
});
}
- (void)task7_3
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"7_3_begin");
sleep(4);
NSLog(@"7_3_end");
dispatch_semaphore_signal(_semaphore);
});
}
- (void)task7_4
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"7_4_begin");
sleep(4);
NSLog(@"7_4_end");
dispatch_semaphore_signal(_semaphore);
});
}
執行結果:
semaphore 為 1
2016-03-11 16:33:11.957 GCDSample[22394:2943718] 7_1_begin
2016-03-11 16:33:15.959 GCDSample[22394:2943718] 7_1_end
2016-03-11 16:33:15.960 GCDSample[22394:2943717] 7_2_begin
2016-03-11 16:33:19.964 GCDSample[22394:2943717] 7_2_end
2016-03-11 16:33:19.965 GCDSample[22394:2943721] 7_3_begin
2016-03-11 16:33:23.970 GCDSample[22394:2943721] 7_3_end
2016-03-11 16:33:23.970 GCDSample[22394:2943727] 7_4_begin
2016-03-11 16:33:27.975 GCDSample[22394:2943727] 7_4_end
------- ------- ------- ------- ------- ------- ------- ----
semaphore 為 2
2016-03-11 16:33:47.396 GCDSample[22406:2944975] 7_2_begin
2016-03-11 16:33:47.396 GCDSample[22406:2944974] 7_1_begin
2016-03-11 16:33:51.398 GCDSample[22406:2944975] 7_2_end
2016-03-11 16:33:51.398 GCDSample[22406:2944974] 7_1_end
2016-03-11 16:33:51.398 GCDSample[22406:2944976] 7_3_begin
2016-03-11 16:33:51.398 GCDSample[22406:2944983] 7_4_begin
2016-03-11 16:33:55.401 GCDSample[22406:2944976] 7_3_end
2016-03-11 16:33:55.401 GCDSample[22406:2944983] 7_4_end
可以看到,并發執行任務的數量取決于_semaphore = dispatch_semaphore_create(2);
傳入的數字,當為1時,效果如同執行串行隊列。
6,dispatch_set_target_queue 設置隊列優先級
如果有串行隊列A和并行隊列B,隊列A中加入任務1,隊列B中加入任務2、任務3,如果確保1、2、3順序執行呢?可以通過dispatch_set_target_queue設置隊列的優先級,將隊列AB指派到隊列C上,任務123將會在串行隊列C中順序執行。代碼如下:
- (void)test8
{
dispatch_queue_t serialQueue = dispatch_queue_create("com.starming.gcddemo.serialqueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t firstQueue = dispatch_queue_create("com.starming.gcddemo.firstqueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t secondQueue = dispatch_queue_create("com.starming.gcddemo.secondqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(firstQueue, serialQueue);
dispatch_set_target_queue(secondQueue, serialQueue);
dispatch_async(firstQueue, ^{
NSLog(@"1 %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3.f];
});
dispatch_async(secondQueue, ^{
NSLog(@"2 %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:2.f];
});
dispatch_async(secondQueue, ^{
NSLog(@"3 %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:1.f];
});
}
執行結果:
2016-03-11 17:31:41.515 GCDSample[1202:730942] 1 <NSThread: 0x170078340>{number = 2, name = (null)}
2016-03-11 17:31:44.518 GCDSample[1202:730942] 2 <NSThread: 0x170078340>{number = 2, name = (null)}
2016-03-11 17:31:46.520 GCDSample[1202:730942] 3 <NSThread: 0x170078340>{number = 2, name = (null)}
未完待續,Have fun!
參考:
https://github.com/nixzhu/dev-blog/blob/master/2014-04-19-grand-central-dispatch-in-depth-part-1.md