美文网首页
异步执行 + 串行队列

异步执行 + 串行队列

作者: Jean_Lina | 来源:发表于2020-07-06 21:06 被阅读0次
/**
 * 异步执行 + 串行队列
 * 特点:会开启新线程,但是因为任务是串行的,执行完一个任务,再执行下一个任务。
 */
- (void)asyncSerial {
    //打印当前线程
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"asyncSerial---begin");
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            //模拟耗时操作
            [NSThread sleepForTimeInterval:2];
            //打印当前线程
            NSLog(@"1---%@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            //模拟耗时操作
            [NSThread sleepForTimeInterval:2];
            //打印当前线程
            NSLog(@"2---%@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            //模拟耗时操作
            [NSThread sleepForTimeInterval:2];
            //打印当前线程
            NSLog(@"3---%@",[NSThread currentThread]);
        }
    });
    NSLog(@"asyncSerial---end");
}
运行结果:
2020-07-06 20:55:53.020073+0800 GCD[16251:3139547] currentThread---<NSThread: 0x6000005b0180>{number = 1, name = main}
2020-07-06 20:55:53.020181+0800 GCD[16251:3139547] asyncSerial---begin
2020-07-06 20:55:53.020276+0800 GCD[16251:3139547] asyncSerial---end
2020-07-06 20:55:55.022406+0800 GCD[16251:3139638] 1---<NSThread: 0x6000005da900>{number = 7, name = (null)}
2020-07-06 20:55:57.024138+0800 GCD[16251:3139638] 1---<NSThread: 0x6000005da900>{number = 7, name = (null)}
2020-07-06 20:55:59.029368+0800 GCD[16251:3139638] 2---<NSThread: 0x6000005da900>{number = 7, name = (null)}
2020-07-06 20:56:01.032945+0800 GCD[16251:3139638] 2---<NSThread: 0x6000005da900>{number = 7, name = (null)}
2020-07-06 20:56:03.033199+0800 GCD[16251:3139638] 3---<NSThread: 0x6000005da900>{number = 7, name = (null)}
2020-07-06 20:56:05.033421+0800 GCD[16251:3139638] 3---<NSThread: 0x6000005da900>{number = 7, name = (null)}

相关文章

  • 关于多线程GCD 串行/并行、同步/异步

    一、GCD串行/并行队列创建 串行队列: 并行队列: 二、GCD串行/并行队列同步/异步执行 执行内容1: 执行结...

  • iOS GCD随记(一)任务/队列组合

    1.同步执行+串行队列2.同步执行+并行队列3.异步执行+串行队列4.异步执行+并行队列5.同步执行+主队列6.异...

  • iOS多线程:『GCD』详尽总结(四):异步执行 + 串行队列

    4.4 异步执行 + 串行队列 在异步执行 + 串行队列可以看到: 开启了一条新线程(异步执行具备开启新线程的能力...

  • iOS开发-队列和同步异步执行的结果分析

    多线程中的队列有:串行队列,并发队列,全局队列(并发),主队列(串行)。 执行的方法有:同步执行和异步执行。 提到...

  • iOS GCD笔记

    串行队列 并发队列 主队列 全局并发队列 同步执行 异步执行 同步+并发队列 = 没有开启新线程,串行执行任务 s...

  • GCD小总结

    单例模式 串行队列同步/异步执行任务 并发队列同步/异步执行任务 队列组 延时执行 barrier

  • 队列dispatch_queue的使用

    队列分为:串行队列、并发队列和主队列,主队列也叫特殊串行队列,是GCD自带的。 任务的执行分为:同步执行和异步执行...

  • iOSIN-GCD

    队列和同步异步 区别并行队列串行队列主队列同步(sync)没有开启新线程,串行执行任务没有开启新线程,串行执行任务...

  • iOS - 多线程(二) GCD讲解

    目录: 1.GCD简介2.串行队列 + 同步执行3.串行队列 + 异步执行4.并发队列 + 同步执行5.并发队列 ...

  • iOS 多线程-CGD

    串行队列同步执行,不开启新线程,任务按顺序执行 串行队列异步执行,会开启新线程(1个),任务按照顺序执行 并行队列...

网友评论

      本文标题:异步执行 + 串行队列

      本文链接:https://www.haomeiwen.com/subject/xrogqktx.html