首先我们需要两种类型queue: one serial和一个同时:
dispatch_queue_t serialQueue =
dispatch_queue_create("com.matteogobbi.dispex.serial_queue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue =
dispatch_queue_create("com.matteogobbi.dispex.concurrent_queue", DISPATCH_QUEUE_CONCURRENT);
所以,我们可以从第一个实验开始,使用串行队列和所有dispatch_async
函数将我们的块添加到队列中:
/* Dispatch async with serial queue */
NSLog(@"\n\nDISPATCH: Async - QUEUE: Serial");
NSLog(@"block 1 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 1");
});
NSLog(@"block 2 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 2");
});
NSLog(@"block 3 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 3");
});
NSLog(@"block 4 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 4");
});
NSLog(@"block 5 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 5");
});
NSLog(@"block 6 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 6");
});
NSLog(@"block 7 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 7");
});
NSLog(@"block 8 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 8");
});
NSLog(@"block 9 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 9");
});
NSLog(@"block 10 added");
dispatch_async(serialQueue, ^{
NSLog(@"block 10");
});
NSLog(@"ALL BLOCK ADDED TO THE QUEUE --> FUNCTION RETURNED");
调度:异步 - 队列:串行
2014-04-08 14:43:16.468 dispex[4346:60b] 添加块 1
2014-04-08 14:43:16.468 dispex[4346:60b] 块 2 添加
2014-04-08 14:43:16.468 dispex[4346:1303] 块 1
2014-04-08 14:43:16.469 dispex[4346:1303] 块 2
2014-04-08 14:43:16.468 dispex[4346:60b] 块 3 添加
2014-04-08 14:43:16.469 dispex[4346:1303] 块 3
2014-04-08 14:43:16.469 dispex[4346:60b] 块 4 添加
2014-04-08 14:43:16.469 dispex[4346:1303] 块 4
2014-04-08 14:43:16.469 dispex[4346:60b] 块 5 添加
2014-04-08 14:43:16.470 dispex[4346:60b] 块 6 添加
2014-04-08 14:43:16.470 dispex[4346:1303] 块 5
2014-04-08 14:43:16.471 dispex[4346:60b] 添加了块 7
2014-04-08 14:43:16.471 dispex[4346:1303] 块 6
2014-04-08 14:43:16.471 dispex[4346:1303] 块 7
2014-04-08 14:43:16.471 dispex[4346:60b] 块 8 添加
2014-04-08 14:43:16.471 dispex[4346:1303] 块 8
2014-04-08 14:43:16.471 dispex[4346:60b] 添加了第 9 块
2014-04-08 14:43:16.472 dispex[4346:60b] 块 10 添加
2014-04-08 14:43:16.472 dispex[4346:1303] 块 9
2014-04-08 14:43:16.472 dispex[4346:1303] 块 10
2014-04-08 14:43:16.472 dispex[4346:60b] 所有块都添加到队列中
--> 函数返回
如您所见,该块已添加到队列中,但是同时调度员开始执行它们。这是一个特点dispatcher_async
函数,即将块添加到队列中,无需等待它们完成执行。换句话说,如果您正在使用dispatch_async
在函数中,函数立即返回,同时块正在执行。这非常有用!在此示例中,我使用 NSLog 来报告块何时添加到队列中,因此这确实会导致执行速度变慢并导致日志
添加了所有块
在最后。但正如我们稍后将看到的,如果没有日志,它将被写在开头。由于我们使用的是串行队列,因此块按照添加的顺序执行。
Next:
/* Just wait before begin with the next test */
dispatch_group_wait(group_async_serial, DISPATCH_TIME_FOREVER);
/* Dispatch sync with serial queue */
NSLog(@"\n\nDISPATCH: Sync - QUEUE: Serial");
NSLog(@"block 1 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 1");
});
NSLog(@"block 2 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 2");
});
NSLog(@"block 3 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 3");
});
NSLog(@"block 4 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 4");
});
NSLog(@"block 5 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 5");
});
NSLog(@"block 6 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 6");
});
NSLog(@"block 7 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 7");
});
NSLog(@"block 8 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 8");
});
NSLog(@"block 9 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 9");
});
NSLog(@"block 10 added");
dispatch_sync(serialQueue, ^{
NSLog(@"block 10");
});
NSLog(@"ALL BLOCK ADDED TO THE QUEUE --> FUNCTION RETURNED");
调度:同步 - 队列:串行
2014-04-08 14:43:16.473 dispex[4346:60b] 块 1 添加
2014-04-08 14:43:16.474 dispex[4346:60b] 块 1
2014-04-08 14:43:16.474 dispex[4346:60b] 块 2 添加
2014-04-08 14:43:16.474 dispex[4346:60b] 块 2
2014-04-08 14:43:16.475 dispex[4346:60b] 块 3 添加
2014-04-08 14:43:16.475 dispex[4346:60b] 块 3
2014-04-08 14:43:16.475 dispex[4346:60b] 块 4 添加
2014-04-08 14:43:16.475 dispex[4346:60b] 块 4
2014-04-08 14:43:16.476 dispex[4346:60b] 块 5 添加
2014-04-08 14:43:16.476 dispex[4346:60b] 块 5
2014-04-08 14:43:16.476 dispex[4346:60b] 块 6 添加
2014-04-08 14:43:16.477 dispex[4346:60b] 块 6
2014-04-08 14:43:16.477 dispex[4346:60b] 块 7 添加
2014-04-08 14:43:16.477 dispex[4346:60b] 块 7
2014-04-08 14:43:16.477 dispex[4346:60b] 块 8 添加
2014-04-08 14:43:16.478 dispex[4346:60b] 块 8
2014-04-08 14:43:16.478 dispex[4346:60b] 添加了第 9 块
2014-04-08 14:43:16.478 dispex[4346:60b] 块 9
2014-04-08 14:43:16.479 dispex[4346:60b] 块 10 添加
2014-04-08 14:43:16.479 dispex[4346:60b] 块 10
2014-04-08 14:43:16.479 dispex[4346:60b] 所有块都添加到队列中
--> 函数返回
在这个例子中我们使用dispatch_sync
函数与串行队列。很容易看出,当前一个块执行完毕时,所有块都会被添加。这是一个特点dispatch_sync
。换句话说,直到块执行完毕后,该函数才会返回。由于这是一个串行队列,因此这里的顺序也受到尊重。
Next:
/* Dispatch async with concurrent queue */
NSLog(@"\n\nDISPATCH: Async - QUEUE: Concurrent");
dispatch_group_t group_async_concurrent = dispatch_group_create();
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 1");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 2");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 3");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 4");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 5");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 6");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 7");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 8");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 9");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 10");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 11");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 12");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 13");
});
dispatch_group_async(group_async_concurrent, concurrentQueue, ^{
NSLog(@"block 14");
});
NSLog(@"ALL BLOCK ADDED TO THE QUEUE --> FUNCTION RETURNED");
调度:异步 - 队列:并发
2014-04-08 14:43:16.480 dispex[4346:60b] 所有块都添加到队列中
--> 函数返回
2014-04-08 14:43:16.480 dispex[4346:1303] 块 1
2014-04-08 14:43:16.480 dispex[4346:3503] 块 2
2014-04-08 14:43:16.480 dispex[4346:3603] 块 3
2014-04-08 14:43:16.480 dispex[4346:3803] 块 5
2014-04-08 14:43:16.480 dispex[4346:3703] 块 4
2014-04-08 14:43:16.480 dispex[4346:3903] 块 6
2014-04-08 14:43:16.480 dispex[4346:3a03] 块 7
2014-04-08 14:43:16.480 dispex[4346:3b03] 块 8
2014-04-08 14:43:16.482 dispex[4346:1303] 块 9
2014-04-08 14:43:16.483 dispex[4346:3503] 块 10
2014-04-08 14:43:16.483 dispex[4346:3803] 块 12
2014-04-08 14:43:16.483 dispex[4346:3703] 块 13
2014-04-08 14:43:16.483 dispex[4346:3903] 块 14
2014-04-08 14:43:16.483 dispex[4346:3603] 块 11
正如我之前所说,这里我展示了如何工作dispatch_async
但有一个并发队列。
这真的很有趣,因为没有 NSLog 显示何时添加块,您可以看到在执行第一个块之前如何添加所有块。这种行为不是恒定的. 可能会发生 block1 被执行并且紧随其后dispatch_async
finish将所有块添加到队列中,然后继续执行其他块。另一件需要注意的事情是,块是并发执行的,因此它们不遵守添加顺序,而且这种行为不是恒定的,而是取决于 CPU 使用率、性能和许多其他因素。
Next:
/* Just wait before begin with the next test */
dispatch_group_wait(group_async_concurrent, DISPATCH_TIME_FOREVER);
/* Dispatch sync with concurrent queue */
NSLog(@"\n\nDISPATCH: Sync - QUEUE: Concurrent");
NSLog(@"block 1 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 1");
});
NSLog(@"block 2 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 2");
});
NSLog(@"block 3 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 3");
});
NSLog(@"block 4 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 4");
});
NSLog(@"block 5 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 5");
});
NSLog(@"block 6 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 6");
});
NSLog(@"block 7 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 7");
});
NSLog(@"block 8 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 8");
});
NSLog(@"block 9 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 9");
});
NSLog(@"block 10 added");
dispatch_sync(concurrentQueue, ^{
NSLog(@"block 10");
});
NSLog(@"ALL BLOCK ADDED TO THE QUEUE --> FUNCTION RETURNED");
调度:同步 - 队列:并发
2014-04-08 14:43:16.486 dispex[4346:60b] 块 1 添加
2014-04-08 14:43:16.486 dispex[4346:60b] 块 1
2014-04-08 14:43:16.487 dispex[4346:60b] 块 2 添加
2014-04-08 14:43:16.487 dispex[4346:60b] 块 2
2014-04-08 14:43:16.487 dispex[4346:60b] 块 3 添加
2014-04-08 14:43:16.488 dispex[4346:60b] 块 3
2014-04-08 14:43:16.488 dispex[4346:60b] 块 4 添加
2014-04-08 14:43:16.488 dispex[4346:60b] 块 4
2014-04-08 14:43:16.489 dispex[4346:60b] 块 5 添加
2014-04-08 14:43:16.489 dispex[4346:60b] 块 5
2014-04-08 14:43:16.489 dispex[4346:60b] 块 6 添加
2014-04-08 14:43:16.489 dispex[4346:60b] 块 6
2014-04-08 14:43:16.490 dispex[4346:60b] 块 7 添加
2014-04-08 14:43:16.490 dispex[4346:60b] 块 7
2014-04-08 14:43:16.490 dispex[4346:60b] 块 8 添加
2014-04-08 14:43:16.491 dispex[4346:60b] 块 8
2014-04-08 14:43:16.491 dispex[4346:60b] 添加了第 9 块
2014-04-08 14:43:16.491 dispex[4346:60b] 块 9
2014-04-08 14:43:16.492 dispex[4346:60b] 块 10 添加
2014-04-08 14:43:16.492 dispex[4346:60b] 块 10
2014-04-08 14:43:16.492 dispex[4346:60b] 所有块都添加到队列中
--> 函数返回
最后,这里我们使用了dispatch_sync
with 并发队列。它看起来完全等于同步/串行对。在这种情况下就是这样。这种行为的原因是,如果队列是并发的,它没有其他块要执行,因为我们正在使用同步调度程序,因此它会等待添加下一个块,直到实际执行完毕为止。这种类型的情侣(同步/并发) 如果我们也添加到队列中,则很有用,块dispatch_async
。在这种情况下,调度程序可以将其他块添加到要执行的队列中。
希望这个迷你演示有用;)
Cheers!