iOS开发CGD

来源:互联网 发布:网易smtp ssl端口 编辑:程序博客网 时间:2024/05/17 23:08
[objc] view plaincopy
  1. //  串行队列 分两种  
  2. //  1.主队列  
  3. //  创建一个主队列  
  4. dispatch_queue_t mainQueue = dispatch_get_main_queue();  
  5. //  像主队列中添加任务  
  6. //  参数1 要添加的队列  
  7. //  参数2 要添加的任务  
  8. dispatch_async(mainQueue, ^{  
  9.     NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);  
  10. });  
  11. dispatch_async(mainQueue, ^{  
  12.     NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);  
  13. });  
  14.   
  15. dispatch_async(mainQueue, ^{  
  16.     NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);  
  17. });  
  18.   
  19. //  任务延迟  
  20. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
  21.       
  22.     NSLog(@"延迟3秒执行");  
  23.       
  24. });  
  25.   
  26. //  ull 是C语言的数值字面量 相当于 unsigned long long  
  27. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55ull * NSEC_PER_SEC), mainQueue, ^{  
  28.     NSLog(@"延迟5秒执行");  
  29. });  
  30.   
  31. //  综上:串行主队列 都在主线冲中进行任务 结束一个 才能进入下一个  


[objc] view plaincopy
  1. //  2.自定义队列  
  2. //  创建一个队列  
  3. //  参数1 自定义队列的标示符 名字  
  4. //  参数2 自定义队列的种类 串行  
  5. dispatch_queue_t myQueue = dispatch_queue_create("com.wl.MyQueue", DISPATCH_QUEUE_SERIAL);  
  6. dispatch_async(myQueue, ^{  
  7.     for (int i = 0; i < 10; i++) {  
  8.         NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  9.     }  
  10. });  
  11. dispatch_async(myQueue, ^{  
  12.     for (int i = 10; i < 20; i++) {  
  13.         NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  14.     }  
  15. });  
  16. dispatch_async(myQueue, ^{  
  17.     for (int i = 20; i < 30; i++) {  
  18.         NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  19.     }  
  20. });  

 
[objc] view plaincopy
  1.     //  创建一个 并行队列  
  2.     //  参数1 设置优先级 无优先级  
  3.     //  参数2 预留参数 一般给0  
  4. //    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  5. //    dispatch_async(myQueue, ^{  
  6. //        for (int i = 0; i < 10; i++) {  
  7. //            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  8. //        }  
  9. //    });  
  10. //    dispatch_async(myQueue, ^{  
  11. //        for (int i = 10; i < 20; i++) {  
  12. //            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  13. //        }  
  14. //    });  
  15. //    dispatch_async(myQueue, ^{  
  16. //        for (int i = 20; i < 30; i++) {  
  17. //            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);  
  18. //        }  
  19. //    });  
  20.   
  21.       
  22.       
  23.     // 2.自定义队列,需要自己手动创建,并设置队列为并行  
  24.     dispatch_queue_t myQueue = dispatch_queue_create("com.lanou3g.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);  
  25.       
  26.     dispatch_async(myQueue, ^{  
  27.         for (int i = 0; i < 10; i++) {  
  28.             NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  29.         }  
  30.     });  
  31.     dispatch_async(myQueue, ^{  
  32.         for (int i = 10; i < 20; i++) {  
  33.             NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  34.         }  
  35.     });  
  36.     dispatch_async(myQueue, ^{  
  37.         for (int i = 20; i < 30; i++) {  
  38.             NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  39.         }  
  40.     });  

[objc] view plaincopy
  1. //  分组队列  
  2. - (void)group  
  3. {  
  4.     // 创建分组  
  5.     dispatch_group_t group = dispatch_group_create();  
  6.     // 创建并行队列(全局队列)  
  7.     dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  8.     // 把myQueue添加到分组group中,并且给myQueue添加任务  
  9.     dispatch_group_async(group, myQueue, ^{  
  10.         for (int i = 0; i < 10; i++) {  
  11.             NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  12.         }  
  13.     });  
  14.     dispatch_group_async(group, myQueue, ^{  
  15.         for (int i = 10; i < 20; i++) {  
  16.             NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  17.         }  
  18.     });  
  19.     // 在分组所有任务执行完成之后,最后指向下面的任务  
  20.     dispatch_group_notify(group, myQueue, ^{  
  21.         // 回到主线程刷新UI  
  22.         dispatch_async(dispatch_get_main_queue(), ^{  
  23.             NSLog(@"当前的线程是: %@", [NSThread currentThread]);  
  24.             NSLog(@"所有数据下载完成.可以去刷新UI了");  
  25.         });  
  26.     });  
  27.       
  28.     dispatch_group_async(group, myQueue, ^{  
  29.         for (int i = 20; i < 30; i++) {  
  30.             NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);  
  31.         }  
  32.     });  
  33.   
  34. }  

[objc] view plaincopy
  1. - (void)barrier  
  2. {  
  3.     // 自己创建并行队列  
  4.     dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.Barrier", DISPATCH_QUEUE_CONCURRENT);  
  5.     dispatch_async(queue, ^{  
  6.         NSLog(@"玩家一读取完成");  
  7.     });  
  8.     dispatch_async(queue, ^{  
  9.         NSLog(@"玩家二读取完成");  
  10.     });  
  11.     dispatch_async(queue, ^{  
  12.         NSLog(@"玩家三读取完成");  
  13.     });  
  14.     // 设置屏障  
  15.     dispatch_barrier_async(queue, ^{  
  16.         NSLog(@"等待其他玩家进入...");  
  17.     });  
  18.       
  19.     // 玩家一进入游戏  
  20.     dispatch_async(queue, ^{  
  21.         NSLog(@"玩家一进入游戏");  
  22.     });  
  23.     // 玩家二进入游戏  
  24.     dispatch_async(queue, ^{  
  25.         NSLog(@"玩家二进入游戏");  
  26.     });  
  27.     // 玩家三进入游戏  
  28.     dispatch_async(queue, ^{  
  29.         NSLog(@"玩家三进入游戏");  
  30.     });  
  31.     dispatch_barrier_async(queue, ^{  
  32.         // 回到主线程,更新UI  
  33.         dispatch_async(dispatch_get_main_queue(), ^{  
  34.             NSLog(@"------%d", [NSThread currentThread].isMainThread);  
  35.             NSLog(@"敌军即将在30秒后进入战场");  
  36.         });  
  37.     });  
  38.       
  39.       
  40. }  
  41.   
  42. //  只执行一次  
  43. - (void)onceBlock  
  44. {  
  45.     static dispatch_once_t onceToken;  
  46.     dispatch_once(&onceToken, ^{  
  47.         NSLog(@"第一滴血");  
  48.     });  

  1. }  



0 0
原创粉丝点击