iOS:对GCD中 同步、异步、并行、串行的见解

1、GCD-同步执行多线程时          GCD中不管向什么类型的队列加同步任务,实际上都会加到当前线程中(一般为主线程)。

2、GCD-异步执行多线程时          GCD中不管向什么类型的队列加同步任务,实际上都会加到新开辟的新线程中(不是主线程)。

举例如下:通过演示线程地址来佐证上述观点.......

情况一:GCD-同步   GCD中向并行队列加同步任务,实际上都会加到当前线程中。

    //当前主线程
    NSLog(@"当前线程:%@",[NSThread currentThread]);//获取一个全局的并行队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
      
    //同步添加任务
    dispatch_sync(queue, ^{
        NSLog(@"任务1,当前线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务2,当前线程:%@",[NSThread currentThread]);
    });

运行结果如下:可以看出这些任务都被加到了mian主线程中,这是所谓的多线程执行成为单一线程执行。

2015-10-08 17:41:12.652 01-GCD-sync[4240:234415] 当前线程:<NSThread: 0x7fba88d16870>{number = 1, name = main}
2015-10-08 17:41:12.652 01-GCD-sync[4240:234415] 任务1,当前线程:<NSThread: 0x7fba88d16870>{number = 1, name = main}
2015-10-08 17:41:12.653 01-GCD-sync[4240:234415] 任务2,当前线程:<NSThread: 0x7fba88d16870>{number = 1, name = main}

====================================================================

情况二:GCD-同步   GCD中向串行队列加同步任务,实际上都会加到当前线程中。

    //当前主线程
    NSLog(@"当前线程:%@",[NSThread currentThread]);

    // 创建一个自定义的串行队列
    dispatch_queue_t myqueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    
    //同步添加任务
    dispatch_sync(myqueue, ^{
        NSLog(@"任务1,当前线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(myqueue, ^{
        NSLog(@"任务2,当前线程:%@",[NSThread currentThread]);
    });

运行结果如下:可以看出这些任务都被加到了main主线程中,这是所谓的多线程执行成为单一线程执行。

2015-10-08 18:27:27.316 01-GCD-sync[4563:251855] 当前线程:<NSThread: 0x7f8633d14370>{number = 1, name = main}
2015-10-08 18:27:27.317 01-GCD-sync[4563:251855] 任务1,当前线程:<NSThread: 0x7f8633d14370>{number = 1, name = main}
2015-10-08 18:27:27.317 01-GCD-sync[4563:251855] 任务2,当前线程:<NSThread: 0x7f8633d14370>{number = 1, name = main}

====================================================================

情况三:GCD-异步  GCD中向并行队列加同步任务,实际上都会加到新开辟的新线程中。

    //当前主线程
    NSLog(@"当前线程:%@",[NSThread currentThread]);

      //获取一个全局的并行队列

     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//异步开启一个新的线程
    dispatch_async(queue, ^{
        
        //新线程
        NSLog(@"新的线程:%@",[NSThread currentThread]);
        
        //在向新线程同步添加任务
        dispatch_sync(queue, ^{
            NSLog(@"任务3,当前线程:%@",[NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"任务4,当前线程:%@",[NSThread currentThread]);
        });
    });

运行结果如下:可以看出此时有两个线程,一个main主线程,一个没名字null的新线程。而这些任务都被加到了新的线程中,两个线程异步执行。

2015-10-08 18:40:09.615 01-GCD-sync[4658:258154] 当前线程:<NSThread: 0x7fcf43f0e750>{number = 1, name = main}
2015-10-08 18:40:09.616 01-GCD-sync[4658:258301] 新的线程:<NSThread: 0x7fcf43c426e0>{number = 2, name = (null)}
2015-10-08 18:40:09.616 01-GCD-sync[4658:258301] 任务3,当前线程:<NSThread: 0x7fcf43c426e0>{number = 2, name = (null)}
2015-10-08 18:40:09.617 01-GCD-sync[4658:258301] 任务4,当前线程:<NSThread: 0x7fcf43c426e0>{number = 2, name = (null)}

====================================================================

情况四:GCD-异步  GCD中向串行队列加同步任务,实际上都会加到新开辟的新线程中。

       //当前主线程
       NSLog(@"当前线程:%@",[NSThread currentThread]);

        // 创建一个自定义的串行队列
        dispatch_queue_t myqueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    
        //异步开启一个新的线程
        dispatch_async(myqueue, ^{
    
            //新线程
            NSLog(@"新线程:%@",[NSThread currentThread]);
    
            //在向新线程同步添加任务
            dispatch_sync(myqueue, ^{
                NSLog(@"任务3,当前线程:%@",[NSThread currentThread]);
            });
            dispatch_sync(myqueue, ^{
                NSLog(@"任务4,当前线程:%@",[NSThread currentThread]);
            });
        });

运行结果如下:情况一样,可以看出此时有两个线程,一个main主线程,一个没名字null的新线程。而这些任务都被加到了新的线程中,两个线程异步执行。

2015-10-08 18:43:49.023 01-GCD-sync[4695:259834] 当前线程:<NSThread: 0x7ffec3f07ca0>{number = 1, name = main}
2015-10-08 18:43:49.024 01-GCD-sync[4695:259921] 新线程:<NSThread: 0x7ffec3e18180>{number = 2, name = (null)}
2015-10-08 18:43:49.024 01-GCD-sync[4695:259921] 任务3,当前线程:<NSThread: 0x7ffec3e18180>{number = 2, name = (null)}
2015-10-08 18:43:49.025 01-GCD-sync[4695:259921] 任务4,当前线程:<NSThread: 0x7ffec3e18180>{number = 2, name = (null)}
原文地址:https://www.cnblogs.com/XYQ-208910/p/4861873.html