使用dispatch_group來進行執行緒同步
一、簡單介紹下將會用到的一些東西
英語不好就不翻譯官方文件了..
1、dispatch_group_async
* Submits a block to a dispatch queue and associates the block with the given
* dispatch group
//將一個block(程式碼塊)加入到dispatch_queue_t queue中並和dispatch_group_t group相關聯
void dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block);
個人理解:將程式碼塊dispatch_block_t block放入佇列dispatch_queue_t queue中執行;並和排程組dispatch_group_t group相互關聯;如果提交到dispatch_queue_t queue中的block全都執行完畢會呼叫dispatch_group_notify並且dispatch_group_wait會停止等待;
2、dispatch_group_enter(group)、dispatch_group_leave(group)
* Calling this function indicates another block has joined the group through
* a means other than dispatch_group_async(). Calls to this function must be
* balanced with dispatch_group_leave().
呼叫這個方法標誌著一個程式碼塊被加入了group,和dispatch_group_async功能類似;dispatch_group_enter()、dispatch_group_leave()必須成對出現
void dispatch_group_enter(dispatch_group_t group);
個人理解:
3、dispatch_group_notify
void dispatch_group_notify(dispatch_group_t group,dispatch_queue_t queue,dispatch_block_t block);
個人理解:當關聯到dispatch_group_t上的dispatch_group_async任務執行完畢或者是關聯在上面的dispatch_group_enter、dispatch_group_leave成對出現了。引數中的dispatch_block_t block會被提交到dispatch_queue_t queue中執行。
4、dispatch_group_wait
long dispatch_group_wait(dispatch_group_t group, dispatch_time_t timeout);
個人理解:和dispatch_group_notify功能類似(多了一個dispatch_time_t引數可以設定超時時間),在group上任務完成前,dispatch_group_wait會阻塞當前執行緒(所以不能放在主執行緒呼叫)一直等待;當group上任務完成,或者等待時間超過設定的超時時間會結束等待;
二、dispatch_group_async程式碼示例
- (void)groupSync
{
dispatch_queue_t disqueue = dispatch_queue_create("com.shidaiyinuo.NetWorkStudy", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t disgroup = dispatch_group_create();
dispatch_group_async(disgroup, disqueue, ^{
NSLog(@"任務一完成");
});
dispatch_group_async(disgroup, disqueue, ^{
sleep(8);
NSLog(@"任務二完成");
});
dispatch_group_notify(disgroup, disqueue, ^{
NSLog(@"dispatch_group_notify 執行");
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_group_wait(disgroup, dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC));
NSLog(@"dispatch_group_wait 結束");
});
}
向group中放入兩個任務(準確講是將任務加入到了並行佇列disqueue中執行,然後佇列和group關聯當佇列上任務執行完畢時group會進行同步),第二個任務會等待8秒所以第一個任務會先完成;會先列印任務一完成再列印任務二完成,當兩個任務都完成時dispatch_group_notify中的block會執行;會接著列印dispatch_group_notify 執行;dispatch_group_wait設定了超時時間為5秒所以它會在5秒後停止等待列印dispatch_group_wait 結束(任務二會等待8秒所以它會在任務二完成前列印);
測試輸出結果需要注意的:dispatch_group_wait是同步的所以不能放在主執行緒執行。
補充:dispatch_group會等和它關聯的所有的dispatch_queue_t上的任務都執行完畢才會發出同步訊號(dispathc_group_notify的程式碼塊block會被執行,group_wati會結束等待)。也就是說一個group可以關聯多個任務佇列;下面給出示例:
- (void)groupSync2
{
dispatch_queue_t dispatchQueue = dispatch_queue_create("ted.queue.next1", 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, ^(){
sleep(5);
NSLog(@"任務一完成");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
sleep(6);
NSLog(@"任務二完成");
});
dispatch_group_async(dispatchGroup, globalQueue, ^{
sleep(10);
NSLog(@"任務三完成");
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"notify:任務都完成了");
});
}
上面的程式碼裡有兩個佇列一個是我自己建立的並行佇列dispatchQueue,另一個是系統提供的並行佇列globalQueue;dispatch_group_notify會等這兩個佇列上的任務都執行完畢才會執行自己的程式碼塊。
多個佇列執行結果三、dispatch_group_enter、dispatch_group_level示例
和dispatch_async相比,當我們呼叫n次dispatch_group_enter後再呼叫n次dispatch_group_level時,dispatch_group_notify和dispatch_group_wait會收到同步訊號;這個特點使得它非常適合處理非同步任務的同步當非同步任務開始前呼叫dispatch_group_enter非同步任務結束後呼叫dispatch_group_leve;
下面是程式碼示例:
- (void)groupSync
{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(5);
NSLog(@"任務一完成");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(8);
NSLog(@"任務二完成");
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"任務完成");
});
}
示例程式碼中在global_queue上執行sleep任務模擬網路請求。
控制檯列印結果補充:如果像最後一個示例那樣,block裡執行的是同步型別的程式碼那麼用dispatch_group_async一樣可以達到同步的效果,但是非同步任務就不行了如下:
- (void)groupSync2
{
dispatch_queue_t dispatchQueue = dispatch_queue_create("ted.queue.next1", 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(5);
NSLog(@"任務一完成");
});
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_async(globalQueue, ^{
sleep(8);
NSLog(@"任務二完成");
});
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"notify:任務都完成了");
});
}
如果dispatch_group_async裡執行的是非同步程式碼dispatch_group_notify會直接觸發而不會等待非同步任務完成,而dispatch_group_enter、和dispatch_group_leave則不會有這個問題,它們只需要在任務開始前enter結束後leave即可達到執行緒同步的效果。