iOS疯狂详解之GCD

    //  串行队列 分两种
    //  1.主队列
    //  创建一个主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    //  像主队列中添加任务
    //  参数1 要添加的队列
    //  参数2 要添加的任务
    dispatch_async(mainQueue, ^{
        NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
    });
    dispatch_async(mainQueue, ^{
        NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
    });
    
    dispatch_async(mainQueue, ^{
        NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
    });
    
    //  任务延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        NSLog(@"延迟3秒执行");
        
    });
    
    //  ull 是C语言的数值字面量 相当于 unsigned long long
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC), mainQueue, ^{
        NSLog(@"延迟5秒执行");
    });
    
    //  综上:串行主队列 都在主线冲中进行任务 结束一个 才能进入下一个


    //  2.自定义队列
    //  创建一个队列
    //  参数1 自定义队列的标示符 名字
    //  参数2 自定义队列的种类 串行
    dispatch_queue_t myQueue = dispatch_queue_create("com.wl.MyQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(myQueue, ^{
        for (int i = 0; i < 10; i++) {
            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
        }
    });
    dispatch_async(myQueue, ^{
        for (int i = 10; i < 20; i++) {
            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
        }
    });
    dispatch_async(myQueue, ^{
        for (int i = 20; i < 30; i++) {
            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
        }
    });

 
    //  创建一个 并行队列
    //  参数1 设置优先级 无优先级
    //  参数2 预留参数 一般给0
//    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_async(myQueue, ^{
//        for (int i = 0; i < 10; i++) {
//            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
//        }
//    });
//    dispatch_async(myQueue, ^{
//        for (int i = 10; i < 20; i++) {
//            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
//        }
//    });
//    dispatch_async(myQueue, ^{
//        for (int i = 20; i < 30; i++) {
//            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
//        }
//    });

    
    
    // 2.自定义队列,需要自己手动创建,并设置队列为并行
    dispatch_queue_t myQueue = dispatch_queue_create("com.lanou3g.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(myQueue, ^{
        for (int i = 0; i < 10; i++) {
            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });
    dispatch_async(myQueue, ^{
        for (int i = 10; i < 20; i++) {
            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });
    dispatch_async(myQueue, ^{
        for (int i = 20; i < 30; i++) {
            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });

//  分组队列
- (void)group
{
    // 创建分组
    dispatch_group_t group = dispatch_group_create();
    // 创建并行队列(全局队列)
    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 把myQueue添加到分组group中,并且给myQueue添加任务
    dispatch_group_async(group, myQueue, ^{
        for (int i = 0; i < 10; i++) {
            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });
    dispatch_group_async(group, myQueue, ^{
        for (int i = 10; i < 20; i++) {
            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });
    // 在分组所有任务执行完成之后,最后指向下面的任务
    dispatch_group_notify(group, myQueue, ^{
        // 回到主线程刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"当前的线程是: %@", [NSThread currentThread]);
            NSLog(@"所有数据下载完成.可以去刷新UI了");
        });
    });
    
    dispatch_group_async(group, myQueue, ^{
        for (int i = 20; i < 30; i++) {
            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
        }
    });

}

- (void)barrier
{
    // 自己创建并行队列
    dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.Barrier", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"玩家一读取完成");
    });
    dispatch_async(queue, ^{
        NSLog(@"玩家二读取完成");
    });
    dispatch_async(queue, ^{
        NSLog(@"玩家三读取完成");
    });
    // 设置屏障
    dispatch_barrier_async(queue, ^{
        NSLog(@"等待其他玩家进入...");
    });
    
    // 玩家一进入游戏
    dispatch_async(queue, ^{
        NSLog(@"玩家一进入游戏");
    });
    // 玩家二进入游戏
    dispatch_async(queue, ^{
        NSLog(@"玩家二进入游戏");
    });
    // 玩家三进入游戏
    dispatch_async(queue, ^{
        NSLog(@"玩家三进入游戏");
    });
    dispatch_barrier_async(queue, ^{
        // 回到主线程,更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"------%d", [NSThread currentThread].isMainThread);
            NSLog(@"敌军即将在30秒后进入战场");
        });
    });
    
    
}

//  只执行一次
- (void)onceBlock
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"第一滴血");
    });
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值