链接:
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"(1).=====%@",[NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"(2).=====%@",[NSThread currentThread]);
});
dispatch_sync(conQueue, ^{
NSLog(@"(3).=====%@",[NSThread currentThread]);
});
(1).=====<NSThread: 0x2837f6f00>{number = 1, name = main}
(2).=====<NSThread: 0x2837f6f00>{number = 1, name = main}
(3).=====<NSThread: 0x2837f6f00>{number = 1, name = main}
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"(1).=====%@",[NSThread currentThread]);
dispatch_async(serialQueue, ^{
NSLog(@"(2).=====%@",[NSThread currentThread]);
});
dispatch_async(conQueue, ^{
NSLog(@"(3).=====%@",[NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"(4).=====%@",[NSThread currentThread]);
});
(1).=====<NSThread: 0x2800a6f00>{number = 1, name = main}
(2).=====<NSThread: 0x2800ca5c0>{number = 3, name = (null)}
(3).=====<NSThread: 0x2800ca5c0>{number = 3, name = (null)}
(4).=====<NSThread: 0x2800a6f00>{number = 1, name = main}
同步:不具备开启线程的能力,一定串行执行任务 异步:具有开启线程的能力,但是在主队列里不会开启新的线程。如果在串行队列和并发队列里开启n个子线程,gcd优化之后未必会真的有n个子线程。