美文网首页
GCD中串行队列和并行队列的同步与异步操作

GCD中串行队列和并行队列的同步与异步操作

作者: 有态度的程序猿 | 来源:发表于2018-08-07 16:13 被阅读0次

GCD中的队列可以归类为以下四种类型,分别进行同步和异步操作

    //串行队列
    dispatch_queue_t serialQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
    //主队列(特殊串行队列)
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    //并行队列
    dispatch_queue_t conQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
    //全局队列(特殊并行队列)
    dispatch_queue_t gloQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

1.同步操作

a.串行队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_sync(serialQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }

运行结果
2018-08-07 16:04:22.260230+0800 TestScrollView[8484:15574025] 任务0运行在主线程:<NSThread: 0x60c00007a2c0>{number = 1, name = main}
2018-08-07 16:04:23.261096+0800 TestScrollView[8484:15574025] 任务1运行在主线程:<NSThread: 0x60c00007a2c0>{number = 1, name = main}
2018-08-07 16:04:24.261354+0800 TestScrollView[8484:15574025] 任务2运行在主线程:<NSThread: 0x60c00007a2c0>{number = 1, name = main}

b.主队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_sync(mainQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[[NSThread currentThread] description]);
            
            sleep(1.0);
            
        });
        
    }
主队列的同步操作由于死锁,没有任何输出

c.并行队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_sync(conQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:07:00.987664+0800 TestScrollView[8664:15586687] 任务0运行在主线程:<NSThread: 0x6000000666c0>{number = 1, name = main}
2018-08-07 16:07:01.987882+0800 TestScrollView[8664:15586687] 任务1运行在主线程:<NSThread: 0x6000000666c0>{number = 1, name = main}
2018-08-07 16:07:02.989140+0800 TestScrollView[8664:15586687] 任务2运行在主线程:<NSThread: 0x6000000666c0>{number = 1, name = main}

d.全局队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_sync(gloQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:08:00.500486+0800 TestScrollView[8736:15591698] 任务0运行在主线程:<NSThread: 0x60400006cec0>{number = 1, name = main}
2018-08-07 16:08:01.501730+0800 TestScrollView[8736:15591698] 任务1运行在主线程:<NSThread: 0x60400006cec0>{number = 1, name = main}
2018-08-07 16:08:02.502040+0800 TestScrollView[8736:15591698] 任务2运行在主线程:<NSThread: 0x60400006cec0>{number = 1, name = main}

综上结果可以发现除了主队列不能运行,其它队列的同步操作都可以正常进行,且都是在主线程上完成,没有生成多线程

2.异步操作

a.串行队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_async(serialQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:08:59.242660+0800 TestScrollView[8811:15596758] 任务0运行在非主线程:<NSThread: 0x60800026c880>{number = 3, name = (null)}
2018-08-07 16:09:00.245155+0800 TestScrollView[8811:15596758] 任务1运行在非主线程:<NSThread: 0x60800026c880>{number = 3, name = (null)}
2018-08-07 16:09:01.250202+0800 TestScrollView[8811:15596758] 任务2运行在非主线程:<NSThread: 0x60800026c880>{number = 3, name = (null)}

b.主队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_async(mainQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:09:32.362832+0800 TestScrollView[8859:15599603] 任务0运行在主线程:<NSThread: 0x600000067bc0>{number = 1, name = main}
2018-08-07 16:09:33.363316+0800 TestScrollView[8859:15599603] 任务1运行在主线程:<NSThread: 0x600000067bc0>{number = 1, name = main}
2018-08-07 16:09:34.364561+0800 TestScrollView[8859:15599603] 任务2运行在主线程:<NSThread: 0x600000067bc0>{number = 1, name = main}

c.并行队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_async(conQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:11:05.467820+0800 TestScrollView[8970:15606571] 任务2运行在非主线程:<NSThread: 0x60800006f9c0>{number = 5, name = (null)}
2018-08-07 16:11:05.467820+0800 TestScrollView[8970:15606573] 任务1运行在非主线程:<NSThread: 0x604000076780>{number = 4, name = (null)}
2018-08-07 16:11:05.467820+0800 TestScrollView[8970:15606582] 任务0运行在非主线程:<NSThread: 0x600000073180>{number = 3, name = (null)}

d.全局队列

for (NSInteger i = 0; i<3; i++) {
        
        dispatch_async(gloQueue, ^{
            
            NSLog(@"任务%zd运行在%@:%@",i,[[NSThread currentThread] isMainThread]?@"主线程":@"非主线程",[NSThread currentThread]);
            
            sleep(1.0);
            
        });
        
    }
运行结果
2018-08-07 16:11:54.530491+0800 TestScrollView[9031:15610818] 任务2运行在非主线程:<NSThread: 0x600000070700>{number = 5, name = (null)}
2018-08-07 16:11:54.530491+0800 TestScrollView[9031:15610821] 任务1运行在非主线程:<NSThread: 0x604000463a80>{number = 4, name = (null)}
2018-08-07 16:11:54.530509+0800 TestScrollView[9031:15610816] 任务0运行在非主线程:<NSThread: 0x60c000073e40>{number = 3, name = (null)}

1.异步操作除了在主队列中不会产生新的线程,在其它队列中都会在新线程中运行block
2.异步操作在串行队列中会FIFO依次等待前一个任务结束之后才运行,在并行队列中则会并发执行

相关文章

  • IOS多线程总结

    目录 简述 NSThread GCD操作与队列异步操作并行队列同步操作并行队列同步操作串行队列异步操作串行队列队列...

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

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

  • GCD基础总结一

    上代码~ 同步串行队列 同步并行队列 异步串行队列 异步并行队列 主队列同步 会卡住 主队列异步

  • GCD

    1、同步串行队列 2、同步并行队列 3、异步串行队列 4、异步并行队列 5、死锁 主线程中创建同步串行队列 主线程...

  • GCD中串行队列和并行队列的同步与异步操作

    GCD中的队列可以归类为以下四种类型,分别进行同步和异步操作 1.同步操作 a.串行队列 b.主队列 c.并行队列...

  • 多线程

    1、同步、异步、串行、并行、全局队列、主队列2、Thread、NSOperation、GCD3、锁

  • 多线程

    iOS中的几种多线程GCD1、GCD分为任务和队列,任务(同步,异步)队列(串行,并发),同步串行,同步主队列的情...

  • 【iOS出租屋进阶】之多线程GCD详解

    线程、任务和队列的概念 异步、同步 & 并行、串行的特点 组合 |并行队列|串行队列 |主队列----|----|...

  • gcd多线程任务与队列组合分析

    关于gcd中串行队列并行队列,以及同步任务和异步任务的花式嵌套,分析执行结果 多线程调试常用代码: gcd的任务 ...

  • 关于GCD总结

    什么是GCD 理解串行、并发及同步异步 串行和并发 同步和异步 队列 串行队列 两者等效.2)使用主队列(在主队列...

网友评论

      本文标题:GCD中串行队列和并行队列的同步与异步操作

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