• GCD详情


      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 
      5 @end
      6 
      7 @implementation ViewController
      8 
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     // Do any additional setup after loading the view, typically from a nib.
     12     //1.多次执行相同的代码块
     13     [self testApply];
     14     //2.代码块只执行一次
     15     //[self testOnce];
     16     //[self testOnce];
     17     //[self testOnce];
     18     //[self testOnce];
     19     //3.代码块在一段时间后执行
     20     //[self testAfter];
     21     //4.将线程放到一个组里面
     22     //[self testGroup];
     23     //5.在两个线程序列之间执行一段代码
     24     //[self testBarrier];
     25 }
     26 
     27 //在两个线程序列之间执行一段代码
     28 -(void)testBarrier
     29 {
     30     //队列不能是系统的全局并行队列
     31     //使用自己创建的并行队列
     32     dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
     33     //线程一
     34     dispatch_async(conQueue, ^{
     35         for (int i=0; i<100; i++) {
     36             NSLog(@"线程一:%d",i);
     37         }
     38     });
     39     
     40     //线程二
     41     dispatch_async(conQueue, ^{
     42         for (int i=0; i<100; i++) {
     43             NSLog(@"线程二:%d",i);
     44         }
     45     });
     46     
     47     //在线程一,线程二执行完成之后,在线程三执行之前执行一段代码
     48     dispatch_barrier_async(conQueue, ^{
     49         NSLog(@"barrier");
     50     });
     51     
     52     //线程三
     53     dispatch_async(conQueue, ^{
     54         for (int i=0; i<100; i++) {
     55             NSLog(@"线程三:%d",i);
     56         }
     57     });
     58 }
     59 
     60 //将线程放到一个组里面
     61 -(void)testGroup
     62 {
     63     dispatch_group_t group = dispatch_group_create();
     64     //获取全局队列
     65     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     66     //参数一:线程所在的组
     67     //参数二:线程所在的队列
     68     //参数三:线程的执行体
     69     //线程一
     70     dispatch_group_async(group, globalQueue, ^{
     71         for (int i=0; i<100; i++) {
     72             NSLog(@"线程一:%d",i);
     73         }
     74     });
     75     //线程二
     76     dispatch_group_async(group, globalQueue, ^{
     77         for (int i=0; i<100; i++) {
     78             NSLog(@"线程二:%d",i);
     79         }
     80     });
     81     
     82     
     83     dispatch_group_notify(group, globalQueue, ^{
     84         //在线程组里的所有线程执行完成后调用的代码
     85         NSLog(@"线程组执行完成");
     86     });
     87 }
     88 
     89 //代码块在一段时间后执行
     90 -(void)testAfter
     91 {
     92     NSLog(@"执行之前");
     93     //时间
     94     //当前时间十秒之后
     95     dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*10);
     96     //参数一:时间
     97     //参数二:线程所在的队列
     98     //参数三:线程的执行体
     99     dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    100         NSLog(@"执行了after");
    101     });
    102 }
    103 
    104 //代码块只执行一次
    105 -(void)testOnce
    106 {
    107     static dispatch_once_t onceToken;
    108     
    109     dispatch_once(&onceToken,^{
    110         NSLog(@"执行一次");
    111     });
    112 }
    113 
    114 //多次执行相同的代码块
    115 -(void)testApply
    116 {
    117     //参数一:代码块执行的次数
    118     //参数二:代码块所在的队列
    119     //参数三:代码块的内容
    120     size_t t = 10;
    121     //队列
    122     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    123     dispatch_apply(t, globalQueue, ^(size_t time) {
    124         NSLog(@"第%ld次",time);
    125     });
    126 }
    127 
    128 - (void)didReceiveMemoryWarning {
    129     [super didReceiveMemoryWarning];
    130     // Dispose of any resources that can be recreated.
    131 }
    132 
    133 @end
  • 相关阅读:
    INTZ DX format
    多线程渲染
    BindVertexbuffer
    Lock flag DX
    Triple buffering and vsync
    可迭代对象和迭代器
    装饰器
    闭包函数
    名称空间和作用域
    函数对象
  • 原文地址:https://www.cnblogs.com/liaods/p/4788931.html
Copyright © 2020-2023  润新知