关于GCD中使用 dispatch_async(异步) 和 dispatch_sync(同步)分别对应DISPATCH_QUEUE_SERIAL(串行)和DISPATCH_QUEUE_CONCURRENT(并行)两种队列的使用情况做一下说明:
A1:如下图为使用串行队列+异步线程情况:
A1:输出结果为
2014-01-29 10:56:54.303 MyCall[5549:60b] STEP_2
2014-01-29 10:56:59.304 MyCall[5549:1f2f] STEP_1
2014-01-29 10:56:59.306 MyCall[5549:1f2f] STEP_3
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A2:如下图为使用并发队列+异步线程情况:
A2:输出结果为
2014-01-29 11:00:44.361 MyCall[5566:60b] STEP_2
2014-01-29 11:00:44.364 MyCall[5566:1303] STEP_3
2014-01-29 11:00:49.362 MyCall[5566:5717] STEP_1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A3:如下图为使用并发队列+同步线程情况:
A3:输出结果为
2014-01-29 11:07:26.446 MyCall[5582:60b] STEP_1
2014-01-29 11:07:26.447 MyCall[5582:60b] STEP_2
2014-01-29 11:07:26.448 MyCall[5582:60b] STEP_3
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A4:如下图为使用串行队列+同步线程情况:(因为同步使用GCD线程会阻塞代码,因此结果同A3完全一样)
A4:输出结果为
2014-01-29 11:07:26.446 MyCall[5582:60b] STEP_12014-01-29 11:07:26.447 MyCall[5582:60b] STEP_2
2014-01-29 11:07:26.448 MyCall[5582:60b] STEP_3
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A5: 如下图为使用并行队列+同步线程情况(两个异步并发线程中分别创建同步线程)