????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)}