各種方法
- 獲取主線程隊列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
- 獲取global子線程隊列
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
關(guān)于子線程隊列priority钞螟,執(zhí)行完High悔醋,再執(zhí)行Default拢锹,然后是Low谣妻,最后是Backgound。
- 創(chuàng)建自定義隊列
dispatch_queue_t customQueue = dispatch_queue_create("com.uzai.testQueue", DISPATCH_QUEUE_CONCURRENT);
創(chuàng)建隊列卒稳,serial串行執(zhí)行蹋半,concurrent并行執(zhí)行
- 異步執(zhí)行
dispatch_async(customQueue, ^{
NSLog(@"custom serial queue %@",[NSThread currentThread]);
});
- 同步執(zhí)行
dispatch_sync(customQueue, ^{
NSLog(@"custom serial queue %@",[NSThread currentThread]);
});
異步執(zhí)行串行隊列,同一子線程串行執(zhí)行
異步執(zhí)行并行隊列充坑,多個子線程并行執(zhí)行
同步執(zhí)行串行隊列减江,主線程串行執(zhí)行
同步執(zhí)行并行隊列染突,主線程串行執(zhí)行
- 按次數(shù)執(zhí)行block
按照指定次數(shù)將 block 添加到指定的隊列當(dāng)中,dispatch_apply函數(shù)會等待全部處理結(jié)束辈灼。
dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSArray *wordsArray = @[@"GCD",@"NSOperations",@"NSOperationQueues",@"algorithms"];
dispatch_apply(wordsArray.count, asyncQueue, ^(size_t currentIndex) {
NSLog(@"current size -> %@",wordsArray[currentIndex]);
});
NSLog(@"all applies are done");
- 創(chuàng)建單例
+ (instancetype)shareInstance {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- dispatch_group
- 使用dispatch_group_async在多個異步任務(wù)完成后份企,再執(zhí)行dispatch_group_notify中的任務(wù)。
dispatch_queue_t current1 = dispatch_queue_create("current1", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, current1, ^{
sleep(3);
NSLog(@"current 1 finish %@",[NSThread currentThread]);
});
dispatch_group_async(group, current1, ^{
sleep(3);
NSLog(@"current 2 finish %@",[NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all finish");
});
//output
//2017-12-07 15:57:59.207 iOSInterviewProblems[64337:3876851] current 1 finish <NSThread: 0x60800026f6c0>{number = 9, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3945895] current 2 finish <NSThread: 0x600000270800>{number = 12, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3875012] all finish
- 使用 dispatch_group_enter巡莹,dispatch_group_leave 獲取多個異步線程執(zhí)行完成時候司志。
- dispatch_group_enter 在任務(wù)開始執(zhí)行時調(diào)用
- dispatch_group_leave 在任務(wù)完成時調(diào)用
dispatch_group_t downloadGroup = dispatch_group_create();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.uzai.download", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 6; i++) {
dispatch_group_enter(downloadGroup);
dispatch_async(downloadQueue, ^{
NSLog(@"now downloading");
sleep(i);
dispatch_group_leave(downloadGroup);
NSLog(@"downloaded");
});
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
NSLog(@"all download done");
});
//output
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995991] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995994] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] downloaded
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996047] now downloading
// 2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996044] now downloading
// 2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996048] now downloading
// 2017-12-07 17:06:25.840 iOSInterviewProblems[65166:3995991] downloaded
// 2017-12-07 17:06:26.766 iOSInterviewProblems[65166:3995994] downloaded
// 2017-12-07 17:06:27.840 iOSInterviewProblems[65166:3996047] downloaded
// 2017-12-07 17:06:28.766 iOSInterviewProblems[65166:3996044] downloaded
// 2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3996048] downloaded
// 2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3995950] all download done
- dispatch_sources
GCD提供一系列的dispatch sources,用來監(jiān)控活動的狀況(底層系統(tǒng)活動降宅,例如Unix 信號骂远,VFS 節(jié)點 等等)。并且在上述活動發(fā)生的時候腰根,執(zhí)行提交到隊列的事務(wù)激才。
-
創(chuàng)建GCDTimer
- gcd timer帶有兩個參數(shù),interval代表的是觸發(fā)時機(jī)额嘿,leeway代表的是允許的誤差瘸恼。例如設(shè)置interval = 1,leeway = 1册养。代表著1秒觸發(fā)东帅,可以有1秒的偏差。
- timer無法自動釋放捕儒,需要通過dispatch_source_cancel進(jìn)行釋放冰啃。
//創(chuàng)建并啟動timer
if (self.timerSource == nil) {
self.timerCountDown = 0;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timerSource, dispatch_walltime(NULL, 0), 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timerSource, ^{
self.timerCountDown++;
NSLog(@"printting now -> %d",self.timerCountDown);
});
dispatch_resume(self.timerSource);
}
//暫停timer
if (self.timerSource) {
dispatch_suspend(self.timerSource);
}
//取消掉timer
if (self.timerSource) {
dispatch_source_cancel(self.timerSource);
}
示例參見 github Demo
注意事項
- dispatch_sync 使用
該方法直到block執(zhí)行完畢才會返回執(zhí)行下一個任務(wù)邓夕。在當(dāng)前隊列調(diào)用會造成死鎖刘莹。