1.異步+并發(fā)
dispatch_queue_t queue = dispatch_queue_create("com.demo.conQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
dispatch_async(queue, ^{
// 還是會按順序執(zhí)行
NSLog(@"%@ 2-1",[NSThread currentThread]);
NSLog(@"%@ 2-2",[NSThread currentThread]);
NSLog(@"%@ 2-3",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"%@ 3",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"%@ 4",[NSThread currentThread]);
});
NSLog(@"5");
// 結果
/*
1
5
<NSThread: 0x60000174dfc0>{number = 3, name = (null)} 2-1
<NSThread: 0x600001704540>{number = 6, name = (null)} 3
<NSThread: 0x600001758f40>{number = 7, name = (null)} 4
<NSThread: 0x60000174dfc0>{number = 3, name = (null)} 2-2
<NSThread: 0x60000174dfc0>{number = 3, name = (null)} 2-3
*/
2.同步/異步 + 串行隊列
dispatch_queue_t queue = dispatch_queue_create("com.demo.serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
// 異步+串行 --> 能夠開辟一條新線程
dispatch_async(queue, ^{
NSLog(@"%@ 2",[NSThread currentThread]);
// 死鎖浅蚪,同步 + 當前隊列 --> 死鎖
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"%@ 4",[NSThread currentThread]);
});
NSLog(@"5");
// 結果
/*
1
5
<NSThread: 0x600001749100>{number = 6, name = (null)} 2
死鎖了
*/