dispatch_sync(),同步添加操做。他是等待添加進隊列裏面的操做完成以後再繼續執行。異步
dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT); NSLog(@"1"); dispatch_sync(concurrentQueue, ^(){ NSLog(@"2"); [NSThread sleepForTimeInterval:10]; NSLog(@"3"); }); NSLog(@"4");
輸出 :
11:36:25.313 GCDSeTest[544:303] 1async
11:36:25.313 GCDSeTest[544:303] 2spa
11:36:30.313 GCDSeTest[544:303] 3//模擬長時間操做code
11:36:30.314 GCDSeTest[544:303] 4blog
dispatch_async ,異步添加進任務隊列,它不會作任何等待隊列
dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT); NSLog(@"1"); dispatch_async(concurrentQueue, ^(){ NSLog(@"2"); [NSThread sleepForTimeInterval:5]; NSLog(@"3"); }); NSLog(@"4");
輸出:
11:42:43.820 GCDSeTest[568:303] 1同步
11:42:43.820 GCDSeTest[568:303] 4class
11:42:43.820 GCDSeTest[568:1003] 2queue
11:42:48.821 GCDSeTest[568:1003] 3//模擬長時間操做時間im