GCD编程dispatch_sync(同步)和dispatch_async(异步)方式执行并发队列任务区别

主线程中【同步执行->并发队列】中的任务

测试方法:
在测试代码主线程中同步提交4个任务给并发队列
观察:任务是哪一个线程负责执行?该执行线程会异步执行这些任务吗?
由程序输出得出结果:
执行任务的线程是:主线程 (同步方法提交任务: 主线程不会开辟子线程去处理新添加在队列中任务)
任务所在的对列是: 并发队列 (不是在跟主线程相关的主队列中)
主线程执行并发队列里面的多个任务时时 是串行执行 也就是是按照任务添加顺序执行的 并且是前面的任务执行完成后再接着执行下一个任务,切记!

主线程中【异步执行->并发队列】中的任务

 测试方法:
 在测试代码主线程中异步提交4个任务给并发队列  
 问题:任务是哪一个线程负责执行?该执行线程会异步执行这些任务吗?
 由程序输出得出结果:
 执行任务的线程是:子线程 (异步方法提交任务: 主线程会新开辟子线程去并发处理这些新添加在队列中任务)
 任务所在的对列是: 并发队列 (不是在跟主线程相关的主队列中)
 新开子线程并发执行并发队列里面的多个任务 也就是说不用等到前面的任务执行完成后再去执行下一个任务

总结
同步方式+并发队列—-> 不开辟新线程,队列中任务是串行执行的
异步方式+并发队列—–>开辟新(子)线程,队列中任务是并发执行的
在并发队列中的多个任务并不是都会被并发的执行,主要取决于:执行这些队列中任务的方式是同步方式(dispatch_sync)还是异步方式(dispatch_async)

下面是测试完整代码:

int main(int argc, const char * argv[]) {
    @autoreleasepool {


        NSLog(@"current_queue is %@!",dispatch_get_current_queue());
        NSLog(@"current_thread is %@",[NSThread currentThread]);

        //获取并发队列
        dispatch_queue_t myqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);



        dispatch_sync(myqueue, ^{
            NSLog(@"1");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
        ;});
        dispatch_sync(myqueue, ^{
            int j= 999999;
            while(j){
                j--;
            }
            NSLog(@"2");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
        });
        dispatch_sync(myqueue, ^{
            NSLog(@"3");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
            int j= 999999;
            while(j){
                j--;
            }
        });
        dispatch_sync(myqueue, ^{
            NSLog(@"4");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
        });

        int j= 999999;
        while(j){
            j--;
        }
        /**程序执行结果:

         2016-09-25 15:12:16.545 ObjectCTest[853:415089] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005c340]>!
         2016-09-25 15:12:16.545 ObjectCTest[853:415089] current_thread is <NSThread: 0x100402cc0>{number = 1, name = main}
         2016-09-25 15:12:16.545 ObjectCTest[853:415089] 1
         2016-09-25 15:12:16.545 ObjectCTest[853:415089] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005c340]>!
         2016-09-25 15:12:16.545 ObjectCTest[853:415089] current_thread is <NSThread: 0x100402cc0>{number = 1, name = main}
         2016-09-25 15:12:16.545 ObjectCTest[853:415089] -----------------------------------------------
         2016-09-25 15:12:16.547 ObjectCTest[853:415089] 2
         2016-09-25 15:12:16.547 ObjectCTest[853:415089] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005c340]>!
         2016-09-25 15:12:16.547 ObjectCTest[853:415089] current_thread is <NSThread: 0x100402cc0>{number = 1, name = main}
         2016-09-25 15:12:16.547 ObjectCTest[853:415089] -----------------------------------------------
         2016-09-25 15:12:16.547 ObjectCTest[853:415089] 3
         2016-09-25 15:12:16.548 ObjectCTest[853:415089] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005c340]>!
         2016-09-25 15:12:16.548 ObjectCTest[853:415089] current_thread is <NSThread: 0x100402cc0>{number = 1, name = main}
         2016-09-25 15:12:16.548 ObjectCTest[853:415089] -----------------------------------------------
         2016-09-25 15:12:16.550 ObjectCTest[853:415089] 4
         2016-09-25 15:12:16.550 ObjectCTest[853:415089] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005c340]>!
         2016-09-25 15:12:16.550 ObjectCTest[853:415089] current_thread is <NSThread: 0x100402cc0>{number = 1, name = main}
         2016-09-25 15:12:16.550 ObjectCTest[853:415089] -----------------------------------------------
 */ 
        NSLog(@"****************************************************");
        //异步提交队列任务
        dispatch_async(myqueue, ^{
            NSLog(@"5");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
            ;});
        dispatch_async(myqueue, ^{
            int j= 999999;
            while(j){
                j--;
            }
            NSLog(@"6");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
        });
        dispatch_async(myqueue, ^{
            int j= 999999;
            while(j){
                j--;
            }
            NSLog(@"7");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");

        });
        dispatch_async(myqueue, ^{
            NSLog(@"8");
            NSLog(@"current_queue is %@!",dispatch_get_current_queue());
            NSLog(@"current_thread is %@",[NSThread currentThread]);
            NSLog(@"-----------------------------------------------");
        });



    }
    //为了防止主线程执行结束直接终结程序导致子线程来不及执行完队列中的任务
    int j= 9999999;
    while(j){
        j--;
    }

    /**程序执行结果
     2016-09-25 15:20:22.255 ObjectCTest[887:440533] ****************************************************
     2016-09-25 15:20:22.256 ObjectCTest[887:440744] 5
     2016-09-25 15:20:22.256 ObjectCTest[887:440744] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005d340]>!
     2016-09-25 15:20:22.256 ObjectCTest[887:440746] 7
     2016-09-25 15:20:22.256 ObjectCTest[887:440747] 8
     2016-09-25 15:20:22.256 ObjectCTest[887:440746] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005d340]>!
     2016-09-25 15:20:22.256 ObjectCTest[887:440747] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005d340]>!
     2016-09-25 15:20:22.256 ObjectCTest[887:440746] current_thread is <NSThread: 0x103a00000>{number = 2, name = (null)}
     2016-09-25 15:20:22.256 ObjectCTest[887:440746] -----------------------------------------------
     2016-09-25 15:20:22.256 ObjectCTest[887:440747] current_thread is <NSThread: 0x1001028f0>{number = 3, name = (null)}
     2016-09-25 15:20:22.257 ObjectCTest[887:440747] -----------------------------------------------
     2016-09-25 15:20:22.257 ObjectCTest[887:440744] current_thread is <NSThread: 0x103800330>{number = 4, name = (null)}
     2016-09-25 15:20:22.257 ObjectCTest[887:440744] -----------------------------------------------
     2016-09-25 15:20:22.258 ObjectCTest[887:440745] 6
     2016-09-25 15:20:22.258 ObjectCTest[887:440745] current_queue is <OS_dispatch_queue_root: com.apple.root.default-qos[0x10005d340]>!
     2016-09-25 15:20:22.258 ObjectCTest[887:440745] current_thread is <NSThread: 0x100503220>{number = 5, name = (null)}
     2016-09-25 15:20:22.258 ObjectCTest[887:440745] -----------------------------------------------
     2016-09-25 15:20:22.274 ObjectCTest[887:440533] 程序执行完毕

     */

    NSLog(@"程序执行完毕");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值