1.串行队列:Dispatch Queues或者 Serial Queues
同步
dispatch_queue_t queue = dispatch_queue_create("zenny_chen_firstQueue", nil);
dispatch_sync(queue, ^(void) {
sleep(2-i/50);
NSLog(@"The sum is: %d", i);
//flag = YES;
});
dispatch_release(queue);
异步
dispatch_queue_t queue = dispatch_queue_create("zenny_chen_firstQueue", nil);
dispatch_async(queue, ^(void) {
sleep(2-i/50);
NSLog(@"The sum is: %d", i);
// signal the semaphore
//dispatch_semaphore_signal(sem);
});
dispatch_release(queue);
当主线程在等待信号量时,操作系统会直接将它挂起,因此不会去死做标志轮询,而能够及时将CPU资源给其它可被调度的线程使用。
__block dispatch_semaphore_t sem = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_queue_create("zenny_chen_firstQueue", nil);
dispatch_async(queue, ^(void) {
sleep(2-i/50);
NSLog(@"The sum is: %d", i);
// signal the semaphore
dispatch_semaphore_signal(sem);
});
// wait for the semaphore
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(queue);
2.并发队列 :Concurrent Queues
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t aHQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);?
dispatch_queue_t aLQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
3.主队列:main dispatch Queue
dispatch_queue_t mainQueue = dispatch_get_main_queue();
针对以上3种队列:concurrent queues 和 main queue 都是由系统生成而且 dispatch_suspend, dispatch_resume, dispatch_set_context 这些函数对他们无效。
但是我们的应用不是简单的同步也异步的运行,应用经常是混合的。
比如我们要task1 task2 task3都运行完成后才能异步运行task4 task5 task6我们该怎么做呢?这里我们可以引入group的概念。
-(void)sixthMethod{
//获取concurrent queue
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建1个queue group
dispatch_group_t queueGroup = dispatch_group_create();
//任务1
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 1.");
});
//任务2
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 2.");
});
//任务3
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 3.");
});
NSLog(@"wait task 1,2,3.");
//等待组内任务全部完成
dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER);
NSLog(@"task 1,2,3 finished.");
//释放组
dispatch_release(queueGroup);
//重新创建组
queueGroup = dispatch_group_create();
//任务4
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 4.");
});
//任务5
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 5.");
});
//任务6
dispatch_group_async(queueGroup, aQueue, ^{
NSLog(@"task 6.");
});
NSLog(@"wait task 4,5,6.");
//等待组内任务全部完成
dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER);
NSLog(@"task 4,5,6 finished.");
//释放组
dispatch_release(queueGroup);
}
代码运行结果:
2013-05-13 13:55:33.783 GDC[2466:1303] task 1.
2013-05-13 13:55:33.783 GDC[2466:3a07] task 3.
2013-05-13 13:55:33.783 GDC[2466:c07] wait task 1,2,3.
2013-05-13 13:55:33.783 GDC[2466:1903] task 2.
2013-05-13 13:55:33.787 GDC[2466:c07] task 1,2,3 finished.
2013-05-13 13:55:33.788 GDC[2466:c07] wait task 4,5,6.
2013-05-13 13:55:33.788 GDC[2466:1303] task 4.
2013-05-13 13:55:33.788 GDC[2466:1903] task 5.
2013-05-13 13:55:33.788 GDC[2466:3a07] task 6.
2013-05-13 13:55:33.790 GDC[2466:c07] task 4,5,6 finished.