iOS 多线程GCD的基本使用
2023-09-27 14:25:01 时间
《iOS多线程简介》中提到: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"); }
造成“相互等待的死锁”
相关文章
- Objective-C 高级编程:iOS与OS X多线程和内存管理
- iOS设置layer阴影
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
- iOS 多线程
- ios的坑 无痕模式
- IOS 手势事件的冲突
- 浅谈 Unity、iOS、Android 闭包的使用方法
- 《iOS 8应用开发入门经典(第6版)》——第1章,第1.4节开发技术概述
- iOS 多线程:『GCD』详尽总结
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用
- XCode下Swift – WebView IOS demo
- 关于iOS多线程,你看我就够了
- iOS中多线程的实现方案
- iOS web与js的简单交互
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
- iOS: 学习笔记, Swift操作符定义
- iOS开发 - 最简洁的用命令行提交代码到Github,加详细说明
- iOS - 处理计算精度要求很高的数据,floatValue,doubleValue等计算不精确问题
- iOS - 获取本地图片和网络图片的大小size
- iOS之UI--关于modal