iOS GCD集汇(三)GCD常用方法まとめ
@崔哥神作を拝読し、最初から最後まで書き直し、コードのサンプルを提供し、皆さんの学習の参考にします.
#pragma mark - Serial Queue
// iOS , GCD, GCD, API, C 。GCD ,GCD dispatch queue , queue 。
- (void)Serial:(UIButton *)sender {
//dispatch queue :serial queue( ) concurrent queue( ); queue FIFO
//serial queue : queue , , , , , , 。
// // serial queue 2 :
// //1、 mainQueue。mainQueue , :
// dispatch_queue_t mainQueue = dispatch_get_main_queue();
// dispatch_async(mainQueue, ^{
// NSLog(@" 1 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(mainQueue, ^{
// NSLog(@" 2 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(mainQueue, ^{
// NSLog(@" 3 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(mainQueue, ^{
// NSLog(@" 4 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(mainQueue, ^{
// NSLog(@" 5 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
//2、 serial queue。// serial queue ,queue ,
dispatch_queue_t mySerialQueue = dispatch_queue_create("com.HMT.GCD.mySerialQueue", DISPATCH_QUEUE_SERIAL);// queue , 。
dispatch_async(mySerialQueue, ^{
NSLog(@" 1 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(mySerialQueue, ^{
NSLog(@" 2 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(mySerialQueue, ^{
NSLog(@" 3 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(mySerialQueue, ^{
NSLog(@" 4 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(mySerialQueue, ^{
NSLog(@" 5 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
}
#pragma mark - Concurrent Queue
- (void)Concurrent:(UIButton *)sender {
//concurrent( )queue 。 : , , , , , , 。 , 。
// // concurrent queue 2 :
// //1、 global queue。
// dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// globalQueue , 4 DISPATCH_QUEUE_PRIORITY_HIGH、DISPATCH_QUEUE_PRIORITY_DEFAULT、DISPATCH_QUEUE_PRIORITY_LOW、DISPATCH_QUEUE_PRIORITY_BACKGROUND。 , , 0.
// //global queue , ( , ), , : , , --- 。
// dispatch_async(globalQueue, ^{
// NSLog(@" 1 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 2 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 3 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 4 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 5 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 6 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 7 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 8 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 9 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
// dispatch_async(globalQueue, ^{
// NSLog(@" 10 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
// });// block ( )
//2、 concurrent queue。
// concurrent queue , ( , ), , : , , --- 。
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.HMT.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 1 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 2 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 3 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 4 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 5 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 6 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 7 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 8 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 9 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
dispatch_async(myConcurrentQueue, ^{
NSLog(@" 10 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});// block ( )
}
#pragma mark - 3
- (void)after:(UIButton *)sender {
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// NSLog(@"Hello");
// });//dispatch_after , mainQueue queue . serial concurrent 。
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.HMT.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_after(popTime, myConcurrentQueue, ^(void){
NSLog(@"world");
});
}
#pragma mark - ,
- (void)group:(UIButton *)sender {
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.HMT.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//dispatch_group_async
//dispatch_group_notify ,
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 1 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 2 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 3 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 4 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_notify(group, myConcurrentQueue, ^{
NSLog(@"group , 。 %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 5 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@" 6 , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
#pragma mark - barrier
- (void)barrier:(UIButton *)sender {
// , , serial queue 。
//serial queue :
// serial queue 。
// , serial queue , 。 concurrent queue 。
// , , 。 ?
// , dispatch_barrier_async
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.HMT.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentQueue, ^{
NSLog(@" , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_barrier_async(myConcurrentQueue, ^{
NSLog(@" , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});//dispatch_barrier_async , , , barrier , 。
dispatch_async(myConcurrentQueue, ^{
NSLog(@" XXX , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" OOXX , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" aaa , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@" bbb , %@, :%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
#pragma mark -
- (void)apply:(UIButton *)sender {
//GCD API 。
NSArray *array = [NSArray arrayWithObjects:@" ",@" ",@" ",@" ", nil];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply([array count], queue, ^(size_t index) {
NSLog(@"%@ %@, :%d",[array objectAtIndex:index],[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
#pragma mark - once
- (void)once:(UIButton *)sender {
//dispatch_once ,
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@" ");
// block , 。
//
// static YourClass *instance = nil;
// instance = [[YourClass alloc] init];
});
}
#pragma mark -
- (void)syn:(id)sender {
// dispatch_async,GCD API dispatch_sync, ?
//dispatch_async block , dispatch_async
//dispatch_sync block , dispatch_sync
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//
// dispatch_sync(queue, ^{
// for (int i = 0; i < 10; i++) {
// NSLog(@"%d",i);
// }
// });
// NSLog(@"haha");
dispatch_async(queue, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"%d",i);
}
});
NSLog(@"haha");
}
#pragma mark - GCD
- (void)functionPoint:(id)sender {
//dispatch_async_f , , block
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async_f(queue, @" ", function);// : , void *。 , dispatch_async_f , 。
}
void function(void *context)
{
NSLog(@"%@ %@, :%d",context,[NSThread currentThread],[[NSThread currentThread] isMainThread]);
}