GCD的常用方法总结

//    GCD常用方法

    //————————————————————————————————————————————————————————————————————————————————

    //串行队列

    dispatch_queue_t queueSerial = dispatch_queue_create("jr", DISPATCH_QUEUE_SERIAL);


    //并行队列

    dispatch_queue_t queueConcu = dispatch_queue_create("jr2", DISPATCH_QUEUE_CONCURRENT);

    //1、循环多次使用

    //(1)添加串行队列,同步执行。可以当for循环使用(在主线程中)

//    dispatch_apply(3, queueSerial, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%@", [NSThread currentThread]);

//    });

    //(2)添加并行队列,同步执行。可以当for循环使用(如果循环次数大于1,则开子线程)

//    dispatch_apply(3, queueConcu, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%@", [NSThread currentThread]);

//    });

    

    //(3)参数size_t的作用:打印的是0.1.2.3.。。的序号

//    dispatch_apply(3, queueSerial, ^(size_t t) {

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%li == %@", t, [NSThread currentThread]);

//    });

    

    //4)以上都是在主线程中进行的。如果想在子线程中执行,需要这样做

//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//        dispatch_apply(3, queueConcu, ^(size_t t) {

//            [NSThread sleepForTimeInterval:1];

//            NSLog(@"%li == %@", t, [NSThread currentThread]);

//        });

//    });

    

    

    //2、分组(可以监听组内子线程是否全部都执行完成)

//    dispatch_group_t group = dispatch_group_create();

//    

//    dispatch_group_async(group, queueConcu, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%@", [NSThread currentThread]);

//    });

//    dispatch_group_async(group, queueConcu, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%@", [NSThread currentThread]);

//    });

//    dispatch_group_async(group, queueConcu, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"%@", [NSThread currentThread]);

//    });

//    

//    dispatch_group_notify(group, queueConcu, ^{

//        NSLog(@"----%@", [NSThread currentThread]);

//    });

    

    

    //3、延迟(写在这个块中的代码都是在主线程中执行的)

    

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

        [NSThread sleepForTimeInterval:2];

        NSLog(@"----%@", [NSThread currentThread]);

    });

     NSLog(@"33333%@", [NSThread currentThread]);

    timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(touchesBegan) userInfo:nil repeats:YES];

   


    //4.设置障碍(在同一队列中,只要添加了障碍,不管有没有创建多线程,他后边的任务都要跨国这个障碍,即等待这个障碍结束)

//    dispatch_async(queueConcu, ^{

//        NSLog(@"11111");

//    });

//    

//    dispatch_barrier_async(queueConcu, ^{

//        [NSThread sleepForTimeInterval:5];

//    });

//    

//    dispatch_async(queueConcu, ^{

//        NSLog(@"11111");

//    });

//    dispatch_async(queueConcu, ^{

//        NSLog(@"11111");

//    });

    

    

    //5、让代码只执行一次

//    static dispatch_once_t oneToken;

//    dispatch_once(&oneToken, ^{

//        NSLog(@"=======");

//    });

    

    //这个方法。常用在单例中


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值