概念
串行队列:加入队列的任务只在一个线程中顺序执行。
并行队列:加入队列的任务可在多个线程中同时执行。
主队列:主线程执行的串行队列。
全局队列:系统定义的全局并行队列。
同步:提交任务后,需要在当前线程立即执行。
异步:提交任务后,不需要立即执行,之后会在子线程中执行。
以下测试代码默认当前线程为主线程。
串行同步
dispatch_queue_t queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"准备任务一");
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务一 %@", [NSThread currentThread]);
}
});
NSLog(@"准备任务二");
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务二 %@", [NSThread currentThread]);
}
});
NSLog(@"结束");
运行结果:当前线程提交任务后立即执行,任务完成后,再继续往下执行。
准备任务一
任务一 <NSThread: 0x600003284980>{number = 1, name = main}
任务一 <NSThread: 0x600003284980>{number = 1, name = main}
任务一 <NSThread: 0x600003284980>{number = 1, name = main}
准备任务二
任务二 <NSThread: 0x600003284980>{number = 1, name = main}
任务二 <NSThread: 0x600003284980>{number = 1, name = main}
任务二 <NSThread: 0x600003284980>{number = 1, name = main}
结束
串行异步
dispatch_queue_t queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"准备任务一");
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务一 %@", [NSThread currentThread]);
}
});
NSLog(@"准备任务二");
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务二 %@", [NSThread currentThread]);
}
});
NSLog(@"结束");
运行结果:提交任务后立即返回,执行完代码逻辑,然后在子线程中按照FIFO的方式执行任务。
准备任务一
准备任务二
结束
任务一 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
任务一 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
任务一 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
任务二 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
任务二 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
任务二 <NSThread: 0x6000021caec0>{number = 6, name = (null)}
并行同步
dispatch_queue_t queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"准备任务一");
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务一 %@", [NSThread currentThread]);
}
});
NSLog(@"准备任务二");
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务二 %@", [NSThread currentThread]);
}
});
NSLog(@"结束");
运行结果:当前线程提交任务后立即执行,任务完成后,再继续往下执行。
准备任务一
任务一 <NSThread: 0x600003f2c540>{number = 1, name = main}
任务一 <NSThread: 0x600003f2c540>{number = 1, name = main}
任务一 <NSThread: 0x600003f2c540>{number = 1, name = main}
准备任务二
任务二 <NSThread: 0x600003f2c540>{number = 1, name = main}
任务二 <NSThread: 0x600003f2c540>{number = 1, name = main}
任务二 <NSThread: 0x600003f2c540>{number = 1, name = main}
结束
并行异步
dispatch_queue_t queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"准备任务一");
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务一 %@", [NSThread currentThread]);
}
});
NSLog(@"准备任务二");
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"任务二 %@", [NSThread currentThread]);
}
});
NSLog(@"结束");
运行结果:提交任务后立即返回,执行完代码逻辑,然后多个子线程同时执行任务。
准备任务一
准备任务二
结束
任务一 <NSThread: 0x6000015633c0>{number = 4, name = (null)}
任务二 <NSThread: 0x600001504180>{number = 8, name = (null)}
任务一 <NSThread: 0x6000015633c0>{number = 4, name = (null)}
任务二 <NSThread: 0x600001504180>{number = 8, name = (null)}
任务一 <NSThread: 0x6000015633c0>{number = 4, name = (null)}
任务二 <NSThread: 0x600001504180>{number = 8, name = (null)}
主队列同步
1.在主线程中运行
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"准备任务一");
dispatch_sync(queue, ^{
NSLog(@"任务一 %@", [NSThread currentThread]);
});
NSLog(@"结束");
运行结果:互相等待造成死锁,程序崩溃。
准备任务一
2.在子线程中运行
NSLog(@"准备任务一");
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"准备任务二");
dispatch_sync(queue, ^{
NSLog(@"任务二 %@", [NSThread currentThread]);
});
NSLog(@"任务一 %@", [NSThread currentThread]);
});
NSLog(@"结束");
运行结果:在子线程中提交任务后,立即在主线程中执行,任务完成后,再继续往下执行。
准备任务一
结束
准备任务二
任务二 <NSThread: 0x600003dc4180>{number = 1, name = main}
任务一 <NSThread: 0x600003dfa6c0>{number = 3, name = (null)}
主队列异步
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"准备任务一");
dispatch_async(queue, ^{
NSLog(@"任务一 %@", [NSThread currentThread]);
});
NSLog(@"准备任务二");
dispatch_async(queue, ^{
NSLog(@"任务二 %@", [NSThread currentThread]);
});
NSLog(@"结束");
运行结果:提交任务后立即返回,执行完代码逻辑,然后在主线程中按照FIFO的方式执行任务。
准备任务一
准备任务二
结束
任务一 <NSThread: 0x6000018044c0>{number = 1, name = main}
任务二 <NSThread: 0x6000018044c0>{number = 1, name = main}
全局队列同步
运行结果同并行同步
全局队列异步
运行结果同并行异步