GCD的一些功能

- (void)viewDidLoad {

    [super viewDidLoad];

    

  // [self groupAction_wait];

  // [self groupAction_notify];

  // [self barrierAction];

    

    

    延迟方法

    [self afterAction];

   开辟新线程

    [self newThreadAct];

    执行一次的操作

    [self onceAction];

}

-(void)groupAction_wait{

    //1.队列

    dispatch_group_t group = dispatch_group_create();

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    dispatch_group_async(group, queue, ^{

        [NSThread sleepForTimeInterval:1];

        NSLog(@"--------1");

    });

    

    dispatch_group_async(group, queue, ^{

        [NSThread sleepForTimeInterval:2];

        NSLog(@"-------2");

    });

    

    dispatch_group_async(group, queue, ^{

        [NSThread sleepForTimeInterval:3];

        NSLog(@"--------3");

    });

    

    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

    NSLog(@"-------4");

}


-(void)groupAction_notify{

    dispatch_group_t group = dispatch_group_create();

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    //先执行---async,再执行----notify

    dispatch_group_async(group, queue, ^{

        [NSThread sleepForTimeInterval:3];

        NSLog(@"---1111");

    });

    dispatch_group_async(group, queue, ^{

        [NSThread sleepForTimeInterval:2];

        NSLog(@"----222");

    });

    dispatch_group_notify(group, queue, ^{

        [NSThread sleepForTimeInterval:1];

        NSLog(@"----333");

    });

    dispatch_group_notify(group, queue, ^{

        [NSThread sleepForTimeInterval:1];

        NSLog(@"---444");

    });

}


-(void)barrierAction{

    dispatch_queue_t queue = dispatch_queue_create("12345", DISPATCH_QUEUE_CONCURRENT);

    //先执行---async,再执行----barrier(barrier和设置的Interval无关,按顺序执行)

    dispatch_async(queue, ^{

        [NSThread sleepForTimeInterval:3];

        NSLog(@"---1");

    });

    

    dispatch_async(queue, ^{

        [NSThread sleepForTimeInterval:2];

        NSLog(@"---4");

    });

    

    dispatch_barrier_async(queue, ^{

        [NSThread sleepForTimeInterval:4];

        NSLog(@"---2");

    });

    

    dispatch_async(queue, ^{

        [NSThread sleepForTimeInterval:4];

        NSLog(@"---8");

    });

    

    dispatch_barrier_async(queue, ^{

        [NSThread sleepForTimeInterval:1];

        NSLog(@"----12");

    });

    NSLog(@"four");

}



-(void)afterAction{

    //延迟方法一

    [self performSelector:@selector(action) withObject:self afterDelay:4];

    

    //不是延迟方法

    [self performSelectorOnMainThread:@selector(aaaction) withObject:self waitUntilDone:YES];

    

    //延迟方法二

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"我是小范范呀");

    });

}


-(void)aaaction{

    NSLog(@"performOnMainThread");

}

-(void)action{

    NSLog(@"performSelector");

}



-(void)newThreadAct{

    // 开辟新线程

    // 将所有卡顿,消耗性能的操作全部丢到新线程中

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSLog(@"例如读取数据的操作。。。。");

        

        // 不要在新线程中去刷新UI

        // 而是要在主线程刷新

        dispatch_async(dispatch_get_main_queue(), ^{

            NSLog(@"cell中的一些控件赋值");

        });


    });

}


-(void)onceAction{


    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"只执行一次的操作放这里");

    });

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值