面试题-同步异步结果

- (void)testAsync1{
    dispatch_queue_t queue = dispatch_queue_create("io", DISPATCH_QUEUE_SERIAL);//并行 串行
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_sync(queue, ^{ //同步异步结果
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    
    NSLog(@"5");
}


- (void)run {
    dispatch_queue_t worker_queue = dispatch_queue_create("worker", DISPATCH_QUEUE_SERIAL);

    NSLog(@"0");

    dispatch_async(worker_queue, ^{
        NSLog(@"1");
    });

    dispatch_async(worker_queue, ^{
        NSLog(@"2");
    });

    dispatch_sync(worker_queue, ^{
        NSLog(@"3");
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"4");
        });
    });

    NSLog(@"5");

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"6");
    });
}

你可能感兴趣的:(面试题-同步异步结果)