iOS:对GCD中 同步、异步、并行、串行的见解
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)}
最新文章
- 简述 OAuth 2.0 的运作流程
- Cesium原理篇:6 Render模块(3: Shader)
- 【转载】SQL Server 2008 r2 中 SQL语句中单引号转义
- 导入导出oracle数据库表的dmp文件
- iOS 学习 - 5.UILabel设置行距
- iPhone尺寸规范
- 使用Boost asio实现异步的TCP/IP通信
- function的粗浅理解
- MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
- AddSelf
- Toy Storage POJ 2398
- ubuntu环境下安装Tomcat
- 解决mariadb grant ERROR 1045 (28000): Access denied for user
- 数据库部分(MySql)_2
- jquery .stop()的用法
- Django开发笔记五
- 虹软人脸识别SDK(java+linux/window)
- L321 How Technology Is Revolutionizing Health Care
- xib中快捷键
- 【JQuery】Ajax
热门文章
- Django 如何实现文件下载
- google code-prettify 代码高亮插件使用方法
- (编译)使用 AppCenter 持续输出导出到 Application Insights
- 找不到 libgtk-x11-2.0.so.0
- js禁止页面所有a链接访问
- Nodejs Mocha测试学习
- Mvc全局过滤器与Action排除
- BootstrapTable与KnockoutJS相结合实现增删改查功能
- 【JavaScript】JS将Java的Timestamp转为Date类型
- grunt-contrib-qunit安装过程中phantomjs安装报错问题解决