GCD队列

作者: CaesarsTesla | 来源:发表于2016-07-27 22:42 被阅读94次

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self syncConcurrent];
}

  • 同步函数+主队列 没有开启新的线程 串行执行任务 会造成线程阻塞 你等我来我等你结果就耗着了
 -(void)syncMain{
    //1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"%s",__func__);
    //2、将任务加进队列
dispatch_sync(queue, ^{
    NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
/*2016-07-27 22:03:18.257 GCD[1474:205778] -[ViewController syncMain]*/
}
  • 异部函数+主队列 只在主线程中执行任务,没有开启新线程 串行执行任务
-(void)asyncMain{
//1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
//2、将任务添加进队列
dispatch_async(queue, ^{
    NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"3----%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:01:12.524 GCD[1461:204697] 1----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 2016-07-27 22:01:12.525 GCD[1461:204697] 2----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 2016-07-27 22:01:12.526 GCD[1461:204697] 3----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
 */
}
  • 同步函数+串行队列 开启新的线程 串行执行任务
-(void)syncSerial{
//1、获得串行队列
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL);
//2、将任务添加到队列中
dispatch_sync(queue, ^{
     NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"3----%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:09:31.890 GCD[1509:209789] 1----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 2016-07-27 22:09:31.891 GCD[1509:209789] 2----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 2016-07-27 22:09:31.892 GCD[1509:209789] 3----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
 */
}
  • 异部函数 + 串行队列 :会开启新的线程,但是任务是串行的,执行完一个任务,在执行下一个任务

    -(void)asyncSerial{
    
    dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
      NSLog(@"1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"2----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"3----%@",[NSThread currentThread]);
    });
    /*
     2016-07-27 22:11:47.582 GCD[1531:211558] 1----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     2016-07-27 22:11:47.584 GCD[1531:211558] 2----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     2016-07-27 22:11:47.585 GCD[1531:211558] 3----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)}
     */
    }
    
  • 同步函数 + 并行队列 :不会开启新的线程,串行执行任务

-(void)syncConcurrent{
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
    NSLog(@"1-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"2-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
    NSLog(@"3-------%@",[NSThread currentThread]);
});
/*
 2016-07-27 22:20:11.875 GCD[1560:215430] 1-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 2016-07-27 22:20:11.877 GCD[1560:215430] 2-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 2016-07-27 22:20:11.878 GCD[1560:215430] 3-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
 */
}
  • 异步函数 + 并行队列:有开启新的线程,并发执行任务

    -(void)asyncConcurrent{
    dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
      NSLog(@"1-------%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"2-------%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
      NSLog(@"3-------%@",[NSThread currentThread]);
    });
    /*
     2016-07-27 22:17:14.859 GCD[1548:213931] 1-------<NSThread: 0x7faaf170fcd0>{number = 5, name = (null)}
     2016-07-27 22:17:14.860 GCD[1548:213866] 2-------<NSThread: 0x7faaf140b5a0>{number = 4, name = (null)}
     2016-07-27 22:17:14.860 GCD[1548:213860] 3-------<NSThread: 0x7faaf14069e0>{number = 2, name = (null)}
     */
    }
    
并发队列 手动创建的串行队列 主队列
同步 没有开启新线程 串行执行任务 没有开启新线程 串行执行任务 没有开启新线程 串行执行任务
异步 有开启新线程 并发执行任务 有开启新线程 串行执行任务 没有开启新线程 串行执行任务

相关文章

  • GCD队列、同步异步

    GCD队列、同步异步 GCD队列、同步异步

  • GCD的学习笔记(One)

    并行和并发 GCD简介 GCD的任务 GCD的队列 GCD创建队列或获取队列的方法 任务的执行方式:同步执行(同步...

  • 2021--- GCD

    gcd同步,异步,串行队列,并发队列,全局队列,主队列,以及死锁。 1、gcd队列阻塞问题[https://www...

  • iOS多线程--GCD篇

    GCD 文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步...

  • GCD串行并发队列

    学习完本篇,您会对以下知识点更加理解: 队列 串行队列 并发队列 GCD全局队列 GCD主队列 创建串行队列 创建...

  • GCD相关知识总结

    一:GCD的队列简介 GCD的Queue有三大队列类型:主队列(main)、全局队列(global)、用户队列(c...

  • 7.多线程基础(七)GCD加强

    1.GCD串行队列和并发队列 2.GCD延时执行 3.GCD线程组:(的作用) 4.GCD定时器: GCD的实现 ...

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

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

  • iOS多线程--彻底学会多线程之『GCD』

    GCD 文章目录 GCD简介 任务和队列 GCD的使用步骤 队列的创建方法 任务的创建方法 GCD的基本使用 并行...

  • iOS GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(六种组合不同区别,队列嵌套情况区别,相互...

网友评论

    本文标题:GCD队列

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