iOS开发CGD
来源:互联网 发布:网易smtp ssl端口 编辑:程序博客网 时间:2024/05/17 23:08
- // 串行队列 分两种
- // 1.主队列
- // 创建一个主队列
- dispatch_queue_t mainQueue = dispatch_get_main_queue();
- // 像主队列中添加任务
- // 参数1 要添加的队列
- // 参数2 要添加的任务
- dispatch_async(mainQueue, ^{
- NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
- });
- dispatch_async(mainQueue, ^{
- NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
- });
- dispatch_async(mainQueue, ^{
- NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
- });
- // 任务延迟
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- NSLog(@"延迟3秒执行");
- });
- // ull 是C语言的数值字面量 相当于 unsigned long long
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55ull * NSEC_PER_SEC), mainQueue, ^{
- NSLog(@"延迟5秒执行");
- });
- // 综上:串行主队列 都在主线冲中进行任务 结束一个 才能进入下一个
- // 2.自定义队列
- // 创建一个队列
- // 参数1 自定义队列的标示符 名字
- // 参数2 自定义队列的种类 串行
- dispatch_queue_t myQueue = dispatch_queue_create("com.wl.MyQueue", DISPATCH_QUEUE_SERIAL);
- dispatch_async(myQueue, ^{
- for (int i = 0; i < 10; i++) {
- NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- }
- });
- dispatch_async(myQueue, ^{
- for (int i = 10; i < 20; i++) {
- NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- }
- });
- dispatch_async(myQueue, ^{
- for (int i = 20; i < 30; i++) {
- NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- }
- });
- // 创建一个 并行队列
- // 参数1 设置优先级 无优先级
- // 参数2 预留参数 一般给0
- // dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- // dispatch_async(myQueue, ^{
- // for (int i = 0; i < 10; i++) {
- // NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- // }
- // });
- // dispatch_async(myQueue, ^{
- // for (int i = 10; i < 20; i++) {
- // NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- // }
- // });
- // dispatch_async(myQueue, ^{
- // for (int i = 20; i < 30; i++) {
- // NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
- // }
- // });
- // 2.自定义队列,需要自己手动创建,并设置队列为并行
- dispatch_queue_t myQueue = dispatch_queue_create("com.lanou3g.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
- dispatch_async(myQueue, ^{
- for (int i = 0; i < 10; i++) {
- NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- dispatch_async(myQueue, ^{
- for (int i = 10; i < 20; i++) {
- NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- dispatch_async(myQueue, ^{
- for (int i = 20; i < 30; i++) {
- NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- // 分组队列
- - (void)group
- {
- // 创建分组
- dispatch_group_t group = dispatch_group_create();
- // 创建并行队列(全局队列)
- dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- // 把myQueue添加到分组group中,并且给myQueue添加任务
- dispatch_group_async(group, myQueue, ^{
- for (int i = 0; i < 10; i++) {
- NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- dispatch_group_async(group, myQueue, ^{
- for (int i = 10; i < 20; i++) {
- NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- // 在分组所有任务执行完成之后,最后指向下面的任务
- dispatch_group_notify(group, myQueue, ^{
- // 回到主线程刷新UI
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"当前的线程是: %@", [NSThread currentThread]);
- NSLog(@"所有数据下载完成.可以去刷新UI了");
- });
- });
- dispatch_group_async(group, myQueue, ^{
- for (int i = 20; i < 30; i++) {
- NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
- }
- });
- }
- - (void)barrier
- {
- // 自己创建并行队列
- dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.Barrier", DISPATCH_QUEUE_CONCURRENT);
- dispatch_async(queue, ^{
- NSLog(@"玩家一读取完成");
- });
- dispatch_async(queue, ^{
- NSLog(@"玩家二读取完成");
- });
- dispatch_async(queue, ^{
- NSLog(@"玩家三读取完成");
- });
- // 设置屏障
- dispatch_barrier_async(queue, ^{
- NSLog(@"等待其他玩家进入...");
- });
- // 玩家一进入游戏
- dispatch_async(queue, ^{
- NSLog(@"玩家一进入游戏");
- });
- // 玩家二进入游戏
- dispatch_async(queue, ^{
- NSLog(@"玩家二进入游戏");
- });
- // 玩家三进入游戏
- dispatch_async(queue, ^{
- NSLog(@"玩家三进入游戏");
- });
- dispatch_barrier_async(queue, ^{
- // 回到主线程,更新UI
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"------%d", [NSThread currentThread].isMainThread);
- NSLog(@"敌军即将在30秒后进入战场");
- });
- });
- }
- // 只执行一次
- - (void)onceBlock
- {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- NSLog(@"第一滴血");
- });
- }
0 0
- iOS开发CGD
- iOS开发CGD买票
- iOS(CGD)
- iOS CGD dispatch_group_t 等待
- IOS中CGD的使用
- iOS多线程简介 - Swift版本 1.多线程开发 -- CGD的简单使用
- iOS CGD 任务开始与结束
- iOS学习笔记-105.多线程04——CGD基础
- GCD开发笔记(一):CGD基本介绍
- CGD应用
- CGD相关
- CGD,信号量
- CGD小计
- IOS多线程编程技术NSThread、Cocoa NSOperation、CGD 三者使用详解
- iOS学习笔记-107.多线程06——CGD线程间通信下载图片
- iOS学习笔记-108.多线程07——CGD栅栏函数、延时、一次性代码
- iOS学习笔记-109.多线程08——CGD快速迭代
- iOS学习笔记-110.多线程09——CGD队列组
- SpringMVC中Shiro报错org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible
- LeetCode题解——Reverse Linked List
- Adapter
- OpenCV 为啥勾搭上 OpenGL?
- CocoaPods pod install/pod update更新慢的问题
- iOS开发CGD
- LeetCode 145 : Binary Tree Postorder Traversal
- magento如何实施正确的缓存策略以达到最佳性能
- MVC日志记录功能
- 基本字符串压缩(程序员面试金典+string)字符串操作
- iOS项目开发实战——使用SDWebImage库进行图片请求
- ipsec openswan资料搜集
- Objective-C之数据持久化-SQLite
- Linux服务器配置for java web