• GCD多线程机制


      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
  • 相关阅读:
    win10设置自带的ubuntu
    C语言动态分配内存及回收
    Qt编写安防视频监控系统47-基本设置
    关于Qt选择qml还是widget的深度思考
    Oracle,查询表空间所有表、表所在的表空间、用户默认表空间,以及如何缩小表空间
    Spring入门小结:HelloWorld程序、依赖注入、Bean的实例化、Bean的作用域、Bean的生命周期、Bean的装配方式
    UltraEdit,Java代码中文乱码问题的解决(包括ANSI编码,以及UTF-8编码的Java源文件)
    MyEclipse,Java代码出现中文乱码问题的解决;以及ASCII/GB2312/GBK文件格式转换为UTF-8文件格式的程序
    按键精灵的几个常见操作:激活窗口/读取Excel/FindPic/组合键输入/等待用户输入/拷贝剪贴板/等待网站返回/读写文本/统计运行时间
    Oracle导入(impdp)比较大的数据,包括创建表空间、创建用户、导入数据等;含expdp及其它
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5321206.html
Copyright © 2020-2023  润新知