_async(ser_queue, ^{
NSLog(@"2-%@",[NSThread currentThread]);
dispatch_sync(con_queue, ^{
NSLog(@"2-%@",[NSThread currentThread]);
});
});
dispatch_async(con_queue, ^{
NSLog(@"3-%@",[NSThread currentThread]);
dispatch_sync(con_queue, ^{
NSLog(@"3-%@",[NSThread currentThread]);
});
});
dispatch_async(con_queue, ^{
NSLog(@"4-%@",[NSThread currentThread]);
dispatch_sync(ser_queue, ^{
NSLog(@"4-%@",[NSThread currentThread]);
});
});
dispatch_async(ser_queue, ^{
NSLog(@"5-%@",[NSThread currentThread]);
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"5-%@",[NSThread currentThread]);
});
});
dispatch_async(con_queue, ^{
NSLog(@"6-%@",[NSThread currentThread]);
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"6-%@",[NSThread currentThread]);
});
});
结论: 结果就不贴出来了,还是自己亲自测下比较好。看看自己的想法和答案是否一致可是一件很快乐的事情。 基本上覆盖了所有可能。感觉更像是面向队列来的,线程的调度是系统自己分配的。
测下来感觉就是回答了两个问题:
- async什么时候会新开线程
- sync什么时候会导致死锁
我的答案:
- 主队列:一定会在主线程
- 串行队列:async会开一条线程
- 并行队列:async会开多条线程
- 死锁:必须是串行队列,其次提交block所在队列和把block放进去的队列是同一个。
- async不同的队列,基本上是在不同的线程上。(存疑)
- 提交block的队列和block放进去的队列是同一个,不管串行并行,都在同一个线程上。(存疑)
GCD是神器,还有好多需要学习的地方,推荐几篇经典的文章: GCD扫盲篇,巧谈GCD GCD进阶篇 死锁,图文并茂,清晰易懂
|