美文网首页
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依次等待前一个任务结束之后才运行,在并行队列中则会并发执行

    相关文章

      网友评论

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

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