GCD--队列的类型和执行任务方式的四种的排列组合

串行队列

  • 串行队列同步执行
- (IBAction)SerialQueueSync:(id)sender {
    //1.创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("FirstSerialQueue", DISPATCH_QUEUE_SERIAL);
    //2.提交任务到队列中(block)
    //同步执行任务
    dispatch_sync(queue, ^{
        //任务逻辑
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第一个打印完毕");
    //第二个任务
    dispatch_sync(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

串行队列同步执行打印结果


  • 串行队列异步执行
- (IBAction)SerialQueueAsync:(id)sender {
    dispatch_queue_t queue = dispatch_queue_create("SecondSerialQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第一个打印完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

串行队列异步执行打印结果


并行队列

  • 并行队列同步执行
//几乎不用这种组合(串行队列+同步执行结果一样)
- (IBAction)concurrentQueueSync:(id)sender {
    dispatch_queue_t queue = dispatch_queue_create("FirstConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第一个打印完毕");
    dispatch_sync(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

并行队列同步执行打印结果


  • 并行队列异步执行
- (IBAction)concurrentQueuAsync:(id)sender {
    dispatch_queue_t queue = dispatch_queue_create("SecondConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@", [NSThread currentThread]);
        }
    });

    NSLog(@"第一个打印完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

并行队列异步执行打印结果


全局队列

  • 全局队列异步执行
//和并行队列异步执行一模一样
- (IBAction)globalQueueAsync:(id)sender {
    //获取系统默认创建的全局队列(参数一:给定队列的优先级)
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //提交任务到全局队列中
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++++:%@",[NSThread currentThread]);
        }
    });
    NSLog(@"第一个打印完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

全局队列异步执行打印结果


主队列

  • 主队列异步执行
- (IBAction)mainQueueAsync:(id)sender {
    //注意:所有主队列中的任务都是由主线程完成
    //获取系统默认创建好的主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //提交任务到主队列中
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第一个打印完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"第二个打印完毕");
}

主队列异步执行打印结果


  • 主队列同步执行
//死锁问题(任务不要放到主队列中并且同步执行)
- (IBAction)mainQueueSync:(id)sender {
    //主队列同步执行
    NSLog(@"任务一");
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"任务二");
    });
    NSLog(@"任务三");


}

主队列同步执行打印结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值