串行队列
添加在队列的任务, 都是按添加顺序先后执行的, 就是所谓的先进先出(FIFO)原则. 我们的主队列就是串行队列, 程序代码按顺序执行. 所以一个串行队列只需要一个线程.
并发队列
添加在该队列中的任务, 会在开启新线程时同时执行, 不开启新线程时, 无法同时执行任务, 就不能实现并发.
同步
任务都在当前线程执行, 执行过程会阻塞当前线程.
异步
有开启新线程的能力, 什么时候开启新线程呢?
- 当使用并发队列时, 肯定会开启新的线程.
- 在新创建的队列中执行异步操作任务, 也会开启新线程.
- 在当前队列中执行异步任务时, 不会开启新线程.
无论哪种情况异步, 都不会阻塞当前线程.
总结
1.同步执行没有开启新线程的能力, 所有的任务都只能在当前线程执行
2.异步执行有开启新线程的能力, 但是, 有开启新线程的能力, 也不一定会利用这种能力, 也就是说, 异步执行是否开启新线程, 需要具体问题具体分析
3.并发队列中的任务会放到不同的线程中去执行.
4.串行队列中的任务只会放到同一线程中去执行.
队列有2种,执行方式有2种
相互组合有四种情况:
串行同步执行
不会开启新线程, 在当前线程中顺序执行任务
代码验证
dispatch_queue_t queue = dispatch_queue_create("serial queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"task1---------%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"task2---------%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"task3---------%@", [NSThread currentThread]);
});
NSLog(@"task4---------%@", [NSThread currentThread]);
输出
task1---------<NSThread: 0x60400006b2c0>{number = 1, name = main}
task2---------<NSThread: 0x60400006b2c0>{number = 1, name = main}
task3---------<NSThread: 0x60400006b2c0>{number = 1, name = main}
task4---------<NSThread: 0x60400006b2c0>{number = 1, name = main}
试试把任务添加在主队列中, 同步执行, 是什么情况? 崩溃在这里解析
串行异步
在当前队列中添加任务, 不会开启新线程, 按顺序执行, 不会阻塞当前线程.
如果新的队列中添加任务, 会开启新线程, 按顺序执行, 不会阻塞当前线程.
代码验证
dispatch_queue_t queue = dispatch_queue_create("serial queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"task1---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task2---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task3---------%@", [NSThread currentThread]);
});
NSLog(@"task4---------%@", [NSThread currentThread]);
输出
task4---------<NSThread: 0x6040000648c0>{number = 1, name = main}
task1---------<NSThread: 0x60000026d740>{number = 3, name = (null)}
task2---------<NSThread: 0x60000026d740>{number = 3, name = (null)}
task3---------<NSThread: 0x60000026d740>{number = 3, name = (null)}
并发同步
不会开启新的线程, 任务按顺序执行, 不能实现并发, 阻塞当前线程
代码验证
dispatch_queue_t queue = dispatch_queue_create("serial queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"task1---------%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"task2---------%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"task3---------%@", [NSThread currentThread]);
});
NSLog(@"task4---------%@", [NSThread currentThread]);
输出
task1---------<NSThread: 0x60400006bb40>{number = 1, name = main}
task2---------<NSThread: 0x60400006bb40>{number = 1, name = main}
task3---------<NSThread: 0x60400006bb40>{number = 1, name = main}
task4---------<NSThread: 0x60400006bb40>{number = 1, name = main}
并发异步
会开启新的线程, 任务同时进行, 实现并发操作, 不会阻塞当前线程
任务是真的同时进行吗? 会的同学请@我, 学习下 是不是真正的并发和 CPU 核数一一对应?
代码验证
dispatch_queue_t queue = dispatch_queue_create("serial queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"task1---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task2---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task3---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task4---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task5---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task6---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task7---------%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"task8---------%@", [NSThread currentThread]);
});
NSLog(@"task9---------%@", [NSThread currentThread]);
输出
task9---------<NSThread: 0x600000077140>{number = 1, name = main}
task1---------<NSThread: 0x600000276b40>{number = 3, name = (null)}
task4---------<NSThread: 0x6000000753c0>{number = 6, name = (null)}
task2---------<NSThread: 0x604000463f80>{number = 4, name = (null)}
task3---------<NSThread: 0x600000276a80>{number = 5, name = (null)}
task5---------<NSThread: 0x604000464040>{number = 7, name = (null)}
task7---------<NSThread: 0x6000000753c0>{number = 6, name = (null)}
task6---------<NSThread: 0x600000276b40>{number = 3, name = (null)}
task8---------<NSThread: 0x604000463f80>{number = 4, name = (null)}
网友评论