美文网首页
并行、串行、同步、异步

并行、串行、同步、异步

作者: 叔叔不吃棒棒糖 | 来源:发表于2017-05-04 17:02 被阅读13次

    多核是并行的前提!

    主队列同步任务

    - (void)queues
    
    {
    
    //创建一个队列为主队列
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    //在主队列中创建一个同步任务,sync是同步的意思
    
    dispatch_sync(queue, ^{
    
    NSLog(@"我是主线程同步任务");
    
    });
    
    }
    

    报错
    原因呢简单点说,主队列是串行队列,其中只有一个主线程,主线程是不会停止的,也就是一直在使用资源。此时开一个同步任务,优先级高,与主线程抢夺资源,导致阻塞了。

    主队列异步任务

    - (void)queues
    {
        //创建一个队列为主队列
        dispatch_queue_t queue = dispatch_get_main_queue();
        //在主队列中创建一个异步任务,sync是同步的意思
        dispatch_async(queue, ^{
            NSLog(@"我是主线程异步任务");
            NSLog(@"是否在主线程%d",[NSThread isMainThread]);
        });
    }
    

    打印结果

    2017-05-04 17:05:51.570 retainCount[13611:947219] 我是主线程异步任务
    2017-05-04 17:05:51.570 retainCount[13611:947219] 是否在主线程1
    

    主线程[NSThread isMainThread]打印出的是1,异步线程优先级低,在cpu空闲时使用资源,不会造成阻塞。

    串行队列异步任务执行顺序

        //创建一个名为first的队列,串行队列,关键词serial,穿行的意思
        dispatch_queue_t queue = dispatch_queue_create("first", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"我是第一个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第二个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第三个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第四个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
    

    打印结果

    2017-05-04 17:40:01.388 retainCount[13981:983486] 我是第一个任务,当前进程<NSThread: 0x600000078f00>{number = 3, name = (null)},是否是主线程0
    2017-05-04 17:40:01.389 retainCount[13981:983486] 我是第二个任务,当前进程<NSThread: 0x600000078f00>{number = 3, name = (null)},是否是主线程0
    2017-05-04 17:40:01.389 retainCount[13981:983486] 我是第三个任务,当前进程<NSThread: 0x600000078f00>{number = 3, name = (null)},是否是主线程0
    2017-05-04 17:40:01.390 retainCount[13981:983486] 我是第四个任务,当前进程<NSThread: 0x600000078f00>{number = 3, name = (null)},是否是主线程0
    

    串行队列中的任务都在一个非主线程中。
    dispatch_queue_create创建一个队列。
    DISPATCH_QUEUE_SERIAL代表串行队列,被分配一个线程。
    串行队列的任务从上到下顺序执行。

    并行异步任务,非顺序执行

        //创建一个名为first的队列,并行队列,关键词concurrent,并行的意思
        dispatch_queue_t queue = dispatch_queue_create("first", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSLog(@"我是第一个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第二个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第三个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"我是第四个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
    

    改为concurrent
    打印结果

    2017-05-04 17:46:07.533 retainCount[14050:991334] 我是第二个任务,当前进程<NSThread: 0x60800027a680>{number = 6, name = (null)},是否是主线程0
    2017-05-04 17:46:07.533 retainCount[14050:991337] 我是第一个任务,当前进程<NSThread: 0x60000026b400>{number = 8, name = (null)},是否是主线程0
    2017-05-04 17:46:07.533 retainCount[14050:991336] 我是第三个任务,当前进程<NSThread: 0x60800027a580>{number = 7, name = (null)},是否是主线程0
    2017-05-04 17:46:07.534 retainCount[14050:991112] 我是第四个任务,当前进程<NSThread: 0x608000276c00>{number = 5, name = (null)},是否是主线程0
    

    并行队列异步任务
    dispatch_queue_create 创建队列
    DISPATCH_QUEUE_ CONCURRENT并行队列
    async异步任务
    并行队列中的任务自动分配到不同线程
    并行队列任务的完成时间基本相同(串行中的任务顺序完成,时间略有不同)

    串行队列同步任务

        //创建一个名为first的队列,串行队列,关键词serial,串行的意思
        dispatch_queue_t queue = dispatch_queue_create("first", DISPATCH_QUEUE_SERIAL);
        //同步任务,关键词sync
        dispatch_sync(queue, ^{
            NSLog(@"我是第一个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第二个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第三个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第四个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
    

    打印结果

    2017-05-04 17:51:30.515 retainCount[14127:996516] 我是第一个任务,当前进程<NSThread: 0x60800006de40>{number = 1, name = main},是否是主线程1
    2017-05-04 17:51:30.516 retainCount[14127:996516] 我是第二个任务,当前进程<NSThread: 0x60800006de40>{number = 1, name = main},是否是主线程1
    2017-05-04 17:51:30.517 retainCount[14127:996516] 我是第三个任务,当前进程<NSThread: 0x60800006de40>{number = 1, name = main},是否是主线程1
    2017-05-04 17:51:30.517 retainCount[14127:996516] 我是第四个任务,当前进程<NSThread: 0x60800006de40>{number = 1, name = main},是否是主线程1
    
    

    同步任务在主线程进行

    并行队列同步任务

        //创建一个名为first的队列,并行队列,关键词concurrent,并行的意思
        dispatch_queue_t queue = dispatch_queue_create("first", DISPATCH_QUEUE_CONCURRENT);
        //同步任务,关键词sync
        dispatch_sync(queue, ^{
            NSLog(@"我是第一个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第二个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第三个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"我是第四个任务,当前进程%@,是否是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
        });
    

    打印结果

    2017-05-04 17:53:41.379 retainCount[14161:998942] 我是第一个任务,当前进程<NSThread: 0x60000007b6c0>{number = 1, name = main},是否是主线程1
    2017-05-04 17:53:41.379 retainCount[14161:998942] 我是第二个任务,当前进程<NSThread: 0x60000007b6c0>{number = 1, name = main},是否是主线程1
    2017-05-04 17:53:41.380 retainCount[14161:998942] 我是第三个任务,当前进程<NSThread: 0x60000007b6c0>{number = 1, name = main},是否是主线程1
    2017-05-04 17:53:41.380 retainCount[14161:998942] 我是第四个任务,当前进程<NSThread: 0x60000007b6c0>{number = 1, name = main},是否是主线程1
    

    并行队列中的同步任务也在主线程

    不论是并行队列还是串行队列,同步任务都在主线程
    【转载并整理于:http://www.jianshu.com/p/a285361a2085

    相关文章

      网友评论

          本文标题:并行、串行、同步、异步

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