GCD (1)

2020-01-10  本文已影响0人  老猫_2017

dispatch_async 与 dispatch_sync 到区别

// 将block 提交到 queueA 上执行,提交到 queueA之后,继续往下执行,不等等block执行完毕,强调提交之后继续往下执行,执行,还是在当前线程进行
dispatch_async(queueA, ^{
    //queueA 线程进行执行
});

// 除非queueA 是dispatch_get_main or target 是 dispatch_get_main, 否则就在当前线程进行执行,做为一个优化操作。直到block 执行完毕之后,才会顺序执行
dispatch_sync(queueA, ^{
    //当前线程进行执行
});

看如下代码,运行如何

dispatch_queue_t queue3 = dispatch_queue_create("t3.0", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"0 %@", NSThread.currentThread);
dispatch_sync(queue3, ^{
    NSLog(@"1 %@", NSThread.currentThread);
    dispatch_sync(queue3, ^{
        NSLog(@"2 %@", NSThread.currentThread);
    });
    NSLog(@"3 %@", NSThread.currentThread);
});

NSLog(@"4 %@", NSThread.currentThread);

输出结果是 0,1,2,3,4, sync 为何没有 死锁,因为是queue3 是并发线程,会有多个执行路径,所以不会死锁,输出的顺序是,提交点的顺序,所以是 按顺序输出, 对应的线程,都是 主线程,这个是线程优化,因为他们跑在主线程中

    dispatch_queue_t queue1 = dispatch_queue_create("t1.0", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue3 = dispatch_queue_create("t3.0", DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"0 %@", NSThread.currentThread);
    dispatch_async(queue1, ^{
        dispatch_sync(queue3, ^{
            NSLog(@"1 %@", NSThread.currentThread);
            dispatch_sync(queue3, ^{
                NSLog(@"2 %@", NSThread.currentThread);
            });
            NSLog(@"3 %@", NSThread.currentThread);
        });
    });
    NSLog(@"4 %@", NSThread.currentThread);

输出结果,同上,只是队列是 queue1 的线程队列中

    dispatch_queue_t queue1 = dispatch_queue_create("t1.0", DISPATCH_QUEUE_SERIAL);
    NSLog(@"0 %@", NSThread.currentThread);
    dispatch_sync(queue1, ^{
        NSLog(@"1 %@", NSThread.currentThread);
        dispatch_sync(queue1, ^{
            NSLog(@"2 %@", NSThread.currentThread);
        });
        NSLog(@"3 %@", NSThread.currentThread);
    });
    NSLog(@"4 %@", NSThread.currentThread);

输出 0,1, 然后卡死,同步到主线程去

    dispatch_queue_t queue1 = dispatch_queue_create("t1.0", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue3 = dispatch_queue_create("t3.0", DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"0 %@", NSThread.currentThread);
    dispatch_async(queue1, ^{
        dispatch_sync(queue2, ^{
            NSLog(@"1 %@", NSThread.currentThread);
            dispatch_sync(dispatch_get_main_queue(), ^{
                NSLog(@"2 %@", NSThread.currentThread);
            });
            NSLog(@"3 %@", NSThread.currentThread);
        });
    });
    NSLog(@"4 %@", NSThread.currentThread);

输出是否会死锁呢?不会,输出顺序 0,4,1,2,3 , dispatch_sync(dispatch_get_main_queue() 会真正的切换到主队列中去的。

    dispatch_queue_t queue = dispatch_queue_create("并行", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        dispatch_async(queue, ^{
            NSLog(@"任务2");
        });
        dispatch_async(queue, ^{
            NSLog(@"任务3");
        });
        dispatch_async(queue, ^{
            NSLog(@"任务4");
        });
        dispatch_async(queue, ^{
            NSLog(@"任务5");
        });
        dispatch_barrier_async(queue, ^{
            NSLog(@"任务1000");
        });
        dispatch_async(queue, ^{
            NSLog(@"任务6");
        });
        dispatch_async(queue, ^{
            NSLog(@"任务7");
        });
        //睡眠2秒
//        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务1");
    });

它的输出结果是,1,2,3,4,5 无顺序在 1000之前, 1000,之后 6,7无顺序,

上一篇下一篇

猜你喜欢

热点阅读