2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 串行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 2
2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 串行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 3
2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 串行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 4
2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 并行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 0
2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 并行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 1
2015-11-23 00:40:36.863 01.GCD演练[1952:3613752] 并行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 2
2015-11-23 00:40:36.864 01.GCD演练[1952:3613752] 并行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 3
2015-11-23 00:40:36.864 01.GCD演练[1952:3613752] 并行队列里的同步任务 <NSThread: 0x7ff833505450>{number = 1, name = main} 4
2.异步操作只在非主线程的线程执行,在串行队列中异步操作会在新建的线程中按顺序执行。
复制代码 dispatch_queue_t q_serial = dispatch_queue_create("my_serial_queue", DISPATCH_QUEUE_SERIAL);
for(int i = 0; i < 5; ++i){
dispatch_async(q_serial, ^{
NSLog(@"串行队列 -- 异步任务 %@ %d", [NSThread currentThread], i);
});
}
因为是异步操作,所以会新建一个线程。又因为加入到串行队列中,所以所有的操作只会按顺序执行。
2015-11-23 01:03:22.372 01.GCD演练[2081:3627139] 串行队列 -- 异步任务 <NSThread: 0x7fb593d42f50>{number = 2, name = (null)} 0
2015-11-23 01:03:23.373 01.GCD演练[2081:3627139] 串行队列 -- 异步任务 <NSThread: 0x7fb593d42f50>{number = 2, name = (null)} 1
2015-11-23 01:03:24.374 01.GCD演练[2081:3627139] 串行队列 -- 异步任务 <NSThread: 0x7fb593d42f50>{number = 2, name = (null)} 2
2015-11-23 01:03:25.375 01.GCD演练[2081:3627139] 串行队列 -- 异步任务 <NSThread: 0x7fb593d42f50>{number = 2, name = (null)} 3
2015-11-23 01:03:26.376 01.GCD演练[2081:3627139] 串行队列 -- 异步任务 <NSThread: 0x7fb593d42f50>{number = 2, name = (null)} 4
3.异步操作,并行队列










