1、GCD-同步执行多线程时          GCD中不管向什么类型的队列加同步任务,实际上都会加到当前线程中(一般为主线程)。

2、GCD-异步执行多线程时          GCD中不管向什么类型的队列加同步任务,实际上都会加到新开辟的新线程中(不是主线程)。

举例如下:通过演示线程地址来佐证上述观点.......

情况一:GCD-同步   GCD中向并行队列加同步任务,实际上都会加到当前线程中。

    //当前主线程
NSLog(@"当前线程:%@",[NSThread currentThread]);//获取一个全局的并行队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); //同步添加任务
dispatch_sync(queue, ^{
NSLog(@"任务1,当前线程:%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务2,当前线程:%@",[NSThread currentThread]);
});

运行结果如下:可以看出这些任务都被加到了mian主线程中,这是所谓的多线程执行成为单一线程执行。

-- ::12.652 -GCD-sync[:] 当前线程:<NSThread: 0x7fba88d16870>{number = , name = main}
-- ::12.652 -GCD-sync[:] 任务1,当前线程:<NSThread: 0x7fba88d16870>{number = , name = main}
-- ::12.653 -GCD-sync[:] 任务2,当前线程:<NSThread: 0x7fba88d16870>{number = , name = main}

====================================================================

情况二:GCD-同步   GCD中向串行队列加同步任务,实际上都会加到当前线程中。

    //当前主线程
NSLog(@"当前线程:%@",[NSThread currentThread]); // 创建一个自定义的串行队列
dispatch_queue_t myqueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT); //同步添加任务
dispatch_sync(myqueue, ^{
NSLog(@"任务1,当前线程:%@",[NSThread currentThread]);
});
dispatch_sync(myqueue, ^{
NSLog(@"任务2,当前线程:%@",[NSThread currentThread]);
});

运行结果如下:可以看出这些任务都被加到了main主线程中,这是所谓的多线程执行成为单一线程执行。

-- ::27.316 -GCD-sync[:] 当前线程:<NSThread: 0x7f8633d14370>{number = , name = main}
-- ::27.317 -GCD-sync[:] 任务1,当前线程:<NSThread: 0x7f8633d14370>{number = , name = main}
-- ::27.317 -GCD-sync[:] 任务2,当前线程:<NSThread: 0x7f8633d14370>{number = , name = main}

====================================================================

情况三:GCD-异步  GCD中向并行队列加同步任务,实际上都会加到新开辟的新线程中。

    //当前主线程
NSLog(@"当前线程:%@",[NSThread currentThread]);

//获取一个全局的并行队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );

//异步开启一个新的线程
dispatch_async(queue, ^{ //新线程
NSLog(@"新的线程:%@",[NSThread currentThread]); //在向新线程同步添加任务
dispatch_sync(queue, ^{
NSLog(@"任务3,当前线程:%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务4,当前线程:%@",[NSThread currentThread]);
});
});

运行结果如下:可以看出此时有两个线程,一个main主线程,一个没名字null的新线程。而这些任务都被加到了新的线程中,两个线程异步执行。

-- ::09.615 -GCD-sync[:] 当前线程:<NSThread: 0x7fcf43f0e750>{number = , name = main}
-- ::09.616 -GCD-sync[:] 新的线程:<NSThread: 0x7fcf43c426e0>{number = , name = (null)}
-- ::09.616 -GCD-sync[:] 任务3,当前线程:<NSThread: 0x7fcf43c426e0>{number = , name = (null)}
-- ::09.617 -GCD-sync[:] 任务4,当前线程:<NSThread: 0x7fcf43c426e0>{number = , name = (null)}

====================================================================

情况四:GCD-异步  GCD中向串行队列加同步任务,实际上都会加到新开辟的新线程中。

       //当前主线程
NSLog(@"当前线程:%@",[NSThread currentThread]); // 创建一个自定义的串行队列
dispatch_queue_t myqueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT); //异步开启一个新的线程
dispatch_async(myqueue, ^{ //新线程
NSLog(@"新线程:%@",[NSThread currentThread]); //在向新线程同步添加任务
dispatch_sync(myqueue, ^{
NSLog(@"任务3,当前线程:%@",[NSThread currentThread]);
});
dispatch_sync(myqueue, ^{
NSLog(@"任务4,当前线程:%@",[NSThread currentThread]);
});
});

运行结果如下:情况一样,可以看出此时有两个线程,一个main主线程,一个没名字null的新线程。而这些任务都被加到了新的线程中,两个线程异步执行。

-- ::49.023 -GCD-sync[:] 当前线程:<NSThread: 0x7ffec3f07ca0>{number = , name = main}
-- ::49.024 -GCD-sync[:] 新线程:<NSThread: 0x7ffec3e18180>{number = , name = (null)}
-- ::49.024 -GCD-sync[:] 任务3,当前线程:<NSThread: 0x7ffec3e18180>{number = , name = (null)}
-- ::49.025 -GCD-sync[:] 任务4,当前线程:<NSThread: 0x7ffec3e18180>{number = , name = (null)}

最新文章

  1. 简述 OAuth 2.0 的运作流程
  2. Cesium原理篇:6 Render模块(3: Shader)
  3. 【转载】SQL Server 2008 r2 中 SQL语句中单引号转义
  4. 导入导出oracle数据库表的dmp文件
  5. iOS 学习 - 5.UILabel设置行距
  6. iPhone尺寸规范
  7. 使用Boost asio实现异步的TCP/IP通信
  8. function的粗浅理解
  9. MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  10. AddSelf
  11. Toy Storage POJ 2398
  12. ubuntu环境下安装Tomcat
  13. 解决mariadb grant ERROR 1045 (28000): Access denied for user
  14. 数据库部分(MySql)_2
  15. jquery .stop()的用法
  16. Django开发笔记五
  17. 虹软人脸识别SDK(java+linux/window)
  18. L321 How Technology Is Revolutionizing Health Care
  19. xib中快捷键
  20. 【JQuery】Ajax

热门文章

  1. Django 如何实现文件下载
  2. google code-prettify 代码高亮插件使用方法
  3. (编译)使用 AppCenter 持续输出导出到 Application Insights
  4. 找不到 libgtk-x11-2.0.so.0
  5. js禁止页面所有a链接访问
  6. Nodejs Mocha测试学习
  7. Mvc全局过滤器与Action排除
  8. BootstrapTable与KnockoutJS相结合实现增删改查功能
  9. 【JavaScript】JS将Java的Timestamp转为Date类型
  10. grunt-contrib-qunit安装过程中phantomjs安装报错问题解决