- 无论队列中所指定的执行的函数是同步还是异步,都会等待前一个任务执行完成后,再调度后面的任务
- 要不要开线程由执行任务决定
- dispatch_sync 不开
- dispatch_async 开
- 开几条线程由谁决定
- 串行队列,异步执行,开几条,由底层线程池决定
- 串行队列,同步执行,不开线程
串行队列,异步执行任务
- (void)gcdDemo1 {
for (NSInteger index = 0; index < 10; index ++) {
dispatch_queue_t queue = dispatch_queue_create("zy", NULL);
dispatch_async(queue, ^{
NSLog(@"%@---%zd",[NSThread currentThread],index);
});
}
}
结果如下:

串行队列,同步执行任务
- (void)gcdDemo2 {
for (NSInteger index = 0; index < 10; index ++) {
dispatch_sync(dispatch_queue_create("zy", DISPATCH_QUEUE_SERIAL), ^{
NSLog(@"%@---%zd",[NSThread currentThread],index);
});
NSLog(@"index = %zd",index);
}
NSLog(@"over");
}
串行队列,异步执行
- (void)gcdDemo3 {
dispatch_queue_t queue = dispatch_queue_create("zy", NULL);
for (NSInteger index = 0; index < 10; index ++) {
dispatch_async(queue, ^{
NSLog(@"%@---%zd",[NSThread currentThread],index);
});
}
}
串行队列,同步
- (void)gcdDemo4 {
dispatch_queue_t queue = dispatch_queue_create("zy", NULL);
for (NSInteger index = 0; index < 10; index ++) {
dispatch_sync(queue, ^{
NSLog(@"%@---%zd",[NSThread currentThread],index);
});
}
}