
iOS 多线程GCD的基本使用
发布日期:2021-05-09 04:04:19
浏览次数:15
分类:博客文章
本文共 3169 字,大约阅读时间需要 10 分钟。
《》中提到:GCD中有2个核心概念:1、任务(执行什么操作)2、队列(用来存放任务)
那么多线程GCD的基本使用有哪些呢?
可以分以下多种情况:
1、异步函数 + 并发队列
/** * 异步函数 + 并发队列:可以同时开启多条线程 */- (void)asyncConcurrent{ // 1.创建一个并发队列 // dispatch_queue_create(const char *label, dispatch_queue_attr_t attr); // label : 相当于队列的名字 // dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_CONCURRENT); // 1.获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.将任务加入队列 dispatch_async(queue, ^{ for (NSInteger i = 0; i<3; i++) { NSLog(@"this is first %@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (NSInteger i = 0; i<3; i++) { NSLog(@"this is second %@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (NSInteger i = 0; i<3; i++) { NSLog(@"this is third %@",[NSThread currentThread]); } }); }
2、同步函数 + 并发队列
/** * 同步函数 + 并发队列:不会开启新的线程 */- (void)syncConcurrent{ // 1.获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.将任务加入队列 dispatch_sync(queue, ^{ NSLog(@"this is first %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"this is second %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"this is third %@",[NSThread currentThread]); }); }
3、异步函数 + 串行队列
/** * 异步函数 + 串行队列:会开启新的线程,但是任务是串行的,执行完一个任务,再执行下一个任务 */- (void)asyncSerial{ // 1.创建串行队列 dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_SERIAL); // dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", NULL); // 2.将任务加入队列 dispatch_async(queue, ^{ NSLog(@"this is first %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"this is second %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"this is third %@",[NSThread currentThread]); });}
4、同步函数 + 串行队列
/** * 同步函数 + 串行队列:不会开启新的线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务 */- (void)syncSerial{ // 1.创建串行队列 dispatch_queue_t queue = dispatch_queue_create("com.kyle.queue", DISPATCH_QUEUE_SERIAL); // 2.将任务加入队列 dispatch_sync(queue, ^{ NSLog(@"this is first %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"this is second %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"this is third %@",[NSThread currentThread]); });}
5、异步函数 + 主队列
// * 异步函数 + 主队列:只在主线程中执行任务- (void)asyncMain{ // 1.获得主队列 dispatch_queue_t queue = dispatch_get_main_queue(); // 2.将任务加入队列 dispatch_async(queue, ^{ NSLog(@"this is first %@",[NSThread currentThread]); NSLog(@"this is second %@",[NSThread currentThread]); NSLog(@"this is third %@",[NSThread currentThread]); });}
6、同步函数 + 主队列
// * 同步函数 + 主队列:- (void)syncMain{ NSLog(@"begin"); // 1.获得主队列 dispatch_queue_t queue = dispatch_get_main_queue(); // 2.将任务加入队列 dispatch_sync(queue, ^{ NSLog(@"this is %@",[NSThread currentThread]); }); NSLog(@"end");}
造成“相互等待的死锁”
发表评论
最新留言
不错!
[***.144.177.141]2025年03月31日 10时01分00秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
汇编指令-MOV与ldr区别(7)
2021-05-09
第3阶段——内核启动分析之start_kernel初始化函数(5)
2021-05-09
12.Linux之输入子系统分析(详解)
2021-05-09
19.QT-事件发送函数sendEvent()、postEvent()
2021-05-09
MyBatis 面试题
2021-05-09
最难的不是递归,是这场面试的有缘无分
2021-05-09
学会了这个开源工具,这就和产品妹子对线!
2021-05-09
源码解析之 Mybatis 对 Integer 参数做了什么手脚?
2021-05-09
oracle使用DBMS_RANDOM包生成随机数据
2021-05-09
[转]收集Oracle UNDO诊断信息脚本
2021-05-09
.NET Core 2.0 Preview 1发布下载和文档
2021-05-09
Qt快速入门之三:Qt项目建立、编译、运行和源码详解
2021-05-09
Qt 布局之三:栅格布局的使用详解
2021-05-09
【QML 快速入门】属性(Properties)
2021-05-09
音视频基础知识---像素格式YUV(转)
2021-05-09
音视频-测试工具推荐
2021-05-09
【设计模式 - 结构型模式】1. 适配器模式
2021-05-09
springboot2配置文件定义${user.name}内容失效问题探究
2021-05-09
C++9018:2333/2235——柠檬汽水(Lemonade Line)
2021-05-09