前言:最近在做SDK方向,对于线程、锁、高并发等关键词使用很频繁。做这个嵌套测试,主要是搞懂代码的执行顺序,从而对线程、队列、任务有更深的理解。
先上结果
image.png开始测试,默认认为读者对GCD有所了解
临界区:gcd的block里的任务,如果嵌套了gcd,那么其余的作用域,我定义为临界区。
一、当前串行队列
self.cxqueue = dispatch_queue_create("cxqueue", DISPATCH_QUEUE_SERIAL);
1. 在当前队列追加同步任务,死锁
//注意是追加任务 是同一个队列 这种类型必定死锁。比如在主线程追加同步任务会死锁,主线程并不特殊,就是个串行队列。
dispatch_async(self.cxqueue, ^{
//不管外边是async还是sync,内部这样嵌套的,必定死锁
dispatch_sync(self.cxqueue, ^{
});
});
//典型死锁,属于上边死锁类型
dispatch_sync(dispatch_get_main_queue(), ^{
});
2. 在当前队列追加异步任务,先执行完临界区任务,再执行追加的任务,不开辟新线程
dispatch_async(self.cxqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
dispatch_async(self.cxqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 - %@",[NSThread currentThread]);
});
dispatch_async(self.cxqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 - %@",[NSThread currentThread]);
});
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 3、4会等待临界区执行完才执行
22:56.16 1 任务开始 - <NSThread: >{number = 6, name = (null)}
22:56.16 2 任务结尾 - <NSThread: >{number = 6, name = (null)}
22:56.66 3 - <NSThread: >{number = 6, name = (null)}
22:57.16 4 - <NSThread: >{number = 6, name = (null)}
3. 在当前队列开启同步串行队列,临界区和同步任务在当前线程顺序执行,不开辟新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(self.cxqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 同步串行 - %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
//[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 同步串行 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 临界区2会等待3、4执行完后才执行
43:26.02 1 任务开始 - <NSThread: >{number = 5, name = (null)}
43:26.52 3 同步串行 - <NSThread: >{number = 5, name = (null)}
43:26.53 4 同步串行 - <NSThread: >{number = 5, name = (null)}
43:26.53 2 任务结尾 - <NSThread: >{number = 5, name = (null)}
4. 在当前队列开启异步串行队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务按加入顺序顺序执行,开启一个新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(self.cxqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 异步串行 - %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 异步串行 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印
任务3 会等待 临界区1执行完
任务4 会等待 任务3执行完
临界区2任务 不会等待3、4执行完,但是肯定在1后边执行
40:02.56 1 任务开始 - <NSThread: >{number = 7, name = (null)}
40:02.56 2 任务结尾 - <NSThread: >{number = 7, name = (null)}
40:03.06 3 异步串行 - <NSThread: >{number = 5, name = (null)}
40:03.06 4 异步串行 - <NSThread: >{number = 5, name = (null)}
5. 在当前队列开启同步并发队列,临界区和同步任务在当前线程顺序执行,不开辟新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(self.cxqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
});
[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 3等1 4等3 2等4
33:40.64 1 任务开始 - <NSThread: >{number = 6, name = (null)}
33:41.65 3 同步并发 - <NSThread: >{number = 6, name = (null)}
33:42.15 4 同步并发 - <NSThread: >{number = 6, name = (null)}
33:43.16 2 任务结尾 - <NSThread: >{number = 6, name = (null)}
6. 在当前队列开启异步并发队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务并发执行,开启多个新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(self.cxqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印
234均等待1部分执行完毕
234互相之间无关联,谁先抢到资源谁就调用
把串行队列想象成主队列,就相当于在主线程开了异步并发队列
37:42.60 1 任务开始 - <NSThread:>{number = 6, name = (null)}
37:43.11 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
37:43.11 4 异步并发 - <NSThread:>{number = 4, name = (null)}
37:43.61 3 异步并发 - <NSThread:>{number = 7, name = (null)}
二、当前并发队列(同步异步结论一样)
self.bfqueue = dispatch_queue_create("bfqueue", DISPATCH_QUEUE_CONCURRENT);
1. 在当前队列追加同步任务,临界区和同步任务在当前线程顺序执行,不开辟新线程
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
dispatch_sync(self.bfqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
});
dispatch_sync(self.bfqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
});
[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印
48:51.16 1 任务开始 - <NSThread:>{number = 7, name = (null)}
48:52.17 3 同步并发 - <NSThread:>{number = 7, name = (null)}
48:52.67 4 同步并发 - <NSThread:>{number = 7, name = (null)}
48:53.67 2 任务结尾 - <NSThread:>{number = 7, name = (null)}
2. 在当前队列追加异步任务,任务无序执行,开辟多个线程
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
dispatch_async(self.bfqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
});
dispatch_async(self.bfqueue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 234必定等1部分执行完 234无序
52:03.50 1 任务开始 - <NSThread:>{number = 5, name = (null)}
52:04.00 2 任务结尾 - <NSThread:>{number = 5, name = (null)}
52:04.51 4 异步并发 - <NSThread:>{number = 7, name = (null)}
52:04.51 3 异步并发 - <NSThread:>{number = 6, name = (null)}
3. 在当前队列开启同步串行队列,临界区和同步任务在当前线程顺序执行,不开辟新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 同步串行 - %@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 同步串行 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 顺序等待
02:38.89 1 任务开始 - <NSThread:>{number = 6, name = (null)}
02:40.40 3 同步串行 - <NSThread:>{number = 6, name = (null)}
02:40.90 4 同步串行 - <NSThread:>{number = 6, name = (null)}
02:40.90 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
4. 在当前队列开启异步串行队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务按加入顺序顺序执行,开启一个新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 异步串行 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
});
dispatch_async(queue, ^{
//[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 异步串行 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印
234必定等1部分结束
23无序,4必定等3结束
05:44.06 1 任务开始 - <NSThread:>{number = 6, name = (null)}
05:45.07 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
05:45.57 3 异步串行 - <NSThread:>{number = 4, name = (null)}
05:46.08 4 异步串行 - <NSThread:>{number = 4, name = (null)}
5. 在当前队列开启同步并发队列,临界区和同步任务在当前线程顺序执行,不开辟新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.5];
});
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印 3等1 4等3 2等4
11:39.67 1 任务开始 - <NSThread:>{number = 6, name = (null)}
11:41.17 3 同步并发 - <NSThread:>{number = 6, name = (null)}
11:42.18 4 同步并发 - <NSThread:>{number = 6, name = (null)}
11:42.18 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
6. 在当前队列开启异步并发队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务并发执行,开启多个新线程
dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(self.bfqueue, ^{
NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:0.5];
NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//[NSThread sleepForTimeInterval:0.5];
NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
});
//[NSThread sleepForTimeInterval:1];
NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
});
打印
234均等待1部分执行完毕
234互相之间无关联,谁先抢到资源谁就调用
14:12.47 1 任务开始 - <NSThread: >{number = 4, name = (null)}
14:13.47 2 任务结尾 - <NSThread: >{number = 4, name = (null)}
14:13.47 4 异步并发 - <NSThread: >{number = 6, name = (null)}
14:13.97 3 异步并发 - <NSThread: >{number = 7, name = (null)}
结束:建议复制代码自己跑跑,干看有点抽象。demo我就不传了,就这些东西,仅供参考,学习。
思考:
- 在什么情况下会遇到数据竞争(data race)问题;
- 数据竞争怎么解决;
- 并发队列如何串行执行;
- 什么时候会用到锁。
网友评论