6個結果搞懂 GCD中 QUEUE 和 async/sync

同一個blockObject async

SSViewController *firstVC = (SSViewController *)self.window.rootViewController;
    [firstVC gcdTestMethod:^{
        for (int i=1; i<5; i++)
        {
            NSString *isMain = [[NSThread currentThread] isMainThread]?@"YES":@"NO";
            NSLog(@"    1 ====% 2d  %@ Main:%@",i,[NSThread currentThread],isMain);
        }
    }];
    [firstVC gcdTestMethod:^{
        for (int i=1; i<5; i++)
        {
            NSString *isMain = [[NSThread currentThread] isMainThread]?@"YES":@"NO";
            NSLog(@"    2 ====% 2d  %@ Main:%@",i,[NSThread currentThread],isMain);
        }
    }];

這裏給6個不一樣實現和不一樣的結果: this

======================== 1 ============================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
    if (!_concurrentQ){
        _concurrentQ =
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
    }
    dispatch_async(_concurrentQ, oneGcdObject);
}


======================== 2 ============================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
    if (!_concurrentQ){
        _concurrentQ =
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
    }
    dispatch_sync(_concurrentQ, oneGcdObject);
}


======================== 3 ============================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
    if (!_concurrentQ){
        _concurrentQ =
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
    }
    dispatch_async(_concurrentQ, oneGcdObject);
}


========================= 4 ===========================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
    if (!_concurrentQ){
        _concurrentQ =
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
    }
    dispatch_sync(_concurrentQ, oneGcdObject);
}


========================= 5 ===========================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
//    if (!_concurrentQ){
        _concurrentQ =
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
//    }
    dispatch_sync(_concurrentQ, oneGcdObject);
}


========================= 6 ===========================
- (void)gcdTestMethod:(gcdBlockObject)oneGcdObject
{
//    if (!_concurrentQ){
        _concurrentQ =
//        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_create(oneQueueName, DISPATCH_QUEUE_SERIAL);
//    }
    dispatch_async(_concurrentQ, oneGcdObject);
}

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

1和2對比: DISPATCH_QUEUE_CONCURRENT 的queue 在使用sync時優先使用主線程(當前線程)
3和4對比: DISPATCH_QUEUE_SERIAL 的queue 不管使用sync仍是aync都將遵循FIFO,但使用sync時優先使用主線程(當前線程)
5和6對比: DISPATCH_QUEUE_CONCURRENT 看似建立不一樣的queue 但因爲都是使用sync,實際上都交給了主線程(當前線程),但不是同一個queue;而DISPATCH_QUEUE_SERIAL 看似沒有遵循FIFO,實際是徹底在兩個不一樣的線程中執行的,這是因爲的確建立了兩個不一樣的queue。
spa

Reference about dispatch_sync in "Grand Central Dispatch (GCD) Reference"
"As an optimization, this function invokes the block on the current thread when possible." 線程

相關文章
相關標籤/搜索