- 以先进先出的方式,并发调度队列中的任务执行
- 如果当前调度的任务是同步执行的,会等待任务执行完成后,再调度后续的任务
- 如果当前调度的任务是异步执行的,同时底层线程池有可用的线程资源,会再新的线程调度后续任务的执行
并发队列 同步执行任务
不开线程 顺序执行
- (void)gcdDemo1
{
dispatch_queue_t queue = dispatch_queue_create("czy", DISPATCH_QUEUE_CONCURRENT);
//异步执行任务
for (NSInteger index = 0; index < 10; index ++)
{
dispatch_sync(queue, ^{
NSLog(@"%zd---%@",index,[NSThread currentThread]);
});
}
NSLog(@"end");
}
并发队列 异步执行任务
开线程 无序的执行
- (void)gcdDemo2
{
dispatch_queue_t queue = dispatch_queue_create("czy", DISPATCH_QUEUE_CONCURRENT);
//异步执行任务
for (NSInteger index = 0; index < 10; index ++)
{
dispatch_async(queue, ^{
NSLog(@"%zd---%@",index,[NSThread currentThread]);
});
}
NSLog(@"end");
}