博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCD多线程机制
阅读量:4951 次
发布时间:2019-06-11

本文共 4916 字,大约阅读时间需要 16 分钟。

1 #import "ViewController.h"  2   3 @interface ViewController ()  4   5 @end  6   7 @implementation ViewController  8   9 - (void)viewDidLoad { 10     [super viewDidLoad]; 11     //向主线程添加一个任务 12     //同步添加任务 13     //GCD 主线程 dispatch_get_main_queue() 14     //NSOperationQueue 主线程 [NSOperationQueue mainQueue] 15 //    dispatch_sync(dispatch_get_main_queue(), ^{ 16 //        NSLog(@"1"); 17 //    }); 18      19     //异步添加任务 20     dispatch_async(dispatch_get_main_queue(), ^{ 21         NSLog(@"%@",[NSThread currentThread]); 22     }); 23      24      25      26 } 27 //串行队列 28 - (IBAction)dispatchQueueSerial:(id)sender { 29     //创建串行队列 30     //指针 31     //dispatch_queue_create(字符串指队列的标识,串行或并行) 32     dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.queue1",DISPATCH_QUEUE_SERIAL ); 33     //添加任务 34     dispatch_async(queue, ^{ 35         for ( int i = 0; i <100; i++) { 36             NSLog(@"1,%@",[NSThread currentThread]); 37         } 38     }); 39     dispatch_async(queue, ^{ 40         for (int i = 0; i < 100; i++) { 41              NSLog(@"2,%@",[NSThread currentThread]); 42         } 43         44     }); 45      46     47      48      49 } 50 //全局队列 51 - (IBAction)Dispatch_global_queue:(id)sender { 52    //苹果预置了全局队列,并行队列 53     //dispatch_get_global_queue(队列优先级,占位0) 54     dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 55     //添加任务 56     dispatch_async(global, ^{ 57         NSLog(@"1,%@",[NSThread currentThread]); 58     }); 59     dispatch_async(global, ^{ 60         NSLog(@"2,%@",[NSThread currentThread]); 61     }); 62      63      64      65 } 66 //并行队列和全局队列是一样的 67 - (IBAction)dispatch_queue_concurrent:(id)sender { 68     dispatch_queue_t conQueue = dispatch_queue_create ("com.lanou3g.concurrent1", DISPATCH_QUEUE_CONCURRENT); 69     //添加任务 70     dispatch_async(conQueue, ^{ 71         for (int i = 0; i < 100; i++) { 72             NSLog(@"1,%@",[NSThread currentThread]); 73  74         } 75     }); 76     dispatch_async(conQueue, ^{ 77         for (int i = 0; i < 100; i++) { 78             NSLog(@"2,%@",[NSThread currentThread]); 79              80         } 81     }); 82 } 83 //延时执行 84 - (IBAction)DISPATCH_AFTER:(id)sender { 85     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 86         NSLog(@"延时,%@",[NSThread currentThread]); 87     }); 88      89      90 } 91 //重复执行 92 - (IBAction)ReceiveMemoryWarning:(id)sender { 93     //记得天机size_t的参数 94     size_t count = 10; 95     dispatch_apply(count, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(size_t i) { 96         NSLog(@"执行的次数:%ld",i); 97     }); 98 } 99 //任务添加到队列后,等待其他任务执行后再执行100 101 - (IBAction)DISPATCH_GROUP_NOTIFY:(id)sender {102     //把线程队列添加到组中103     //dispatch_group_async()向组中添加队列104     //dispatch_group_notify()添加任务,会等待其他任务执行完毕105     //创建线程组106     dispatch_group_t group = dispatch_group_create();107     dispatch_queue_t conQueue = dispatch_queue_create("com.lanou", DISPATCH_QUEUE_CONCURRENT);108     //添加任务109     dispatch_group_async(group, conQueue, ^{110         for (int i = 0; i <100; i++) {111             NSLog(@"1,%@",[NSThread currentThread]);112 113         }114     });115     dispatch_group_async(group, conQueue, ^{116         for (int i = 0; i <100; i++) {117             NSLog(@"2,%@",[NSThread currentThread]);118             119         }120     });121     //监听其他任务122     dispatch_group_notify(group, conQueue, ^{123         NSLog(@"等待其他任务执行后,再执行");124     });125 }126 //当前barrier线程执行时,其他任务停止127 - (IBAction)dispatchbarrieradync:(id)sender {128     dispatch_queue_t conQueue = dispatch_queue_create("com.lanou.conQueue", DISPATCH_QUEUE_CONCURRENT);129     //添加任务130     dispatch_async(conQueue, ^{131         for (int i = 0; i < 100; i++) {132             NSLog(@"1,%@",[NSThread currentThread]);133         }134     });135     dispatch_async(conQueue, ^{136         for (int i = 0; i < 100; i++) {137             NSLog(@"2,%@",[NSThread currentThread]);138         }139     });140     //添加barrier任务141     dispatch_barrier_async(conQueue, ^{142         for (int i = 0; i < 100; i++) {143             NSLog(@"barrier,%@",[NSThread currentThread]);144         }145     });146     dispatch_async(conQueue, ^{147         for (int i = 0; i < 100; i++) {148             NSLog(@"4,%@",[NSThread currentThread]);149         }150     });151     152     153 }154 155 //156 - (IBAction)dispatchAsyncF:(id)sender {157     158     dispatch_async_f(dispatch_get_main_queue(), nil, myFunction);   159     160 }161 //定义函数162 void myFunction(){163     for (int i = 0 ; i <100; i++) {164         NSLog(@"1,%@",[NSThread currentThread]);165     }166 }167 168 - (void)didReceiveMemoryWarning {169     [super didReceiveMemoryWarning];170     // Dispose of any resources that can be recreated.171 }172 173 @end

 

转载于:https://www.cnblogs.com/DevinSMR/p/5321206.html

你可能感兴趣的文章
P3565 [POI2014]HOT-Hotels
查看>>
MongoDB的简单使用
查看>>
hdfs 命令使用
查看>>
prometheus配置
查看>>
定宽320 缩放适配手机屏幕
查看>>
BZOJ 2120 数颜色 【带修改莫队】
查看>>
【noip2004】虫食算——剪枝DFS
查看>>
Codeforces 40 E. Number Table
查看>>
CLR via C#(第3 版)
查看>>
java语法之final
查看>>
关于响应式布局
查看>>
详解ASP.Net 4中的aspnet_regsql.exe
查看>>
python 多进程和多线程的区别
查看>>
hdu1398
查看>>
[android] 网络断开的监听
查看>>
156.Binary Tree Upside Down
查看>>
MongoDB在windows下安装配置
查看>>
Upselling promotion stored procedure
查看>>
mysql编码配置
查看>>
KVM地址翻译流程及EPT页表的建立过程
查看>>