同步和异步:
同步:嚣张跋扈,立刻马上执行,如果我没执行完,大家都别执行
异步:谦虚礼让,我没有执行,其他人可以执行
1)同步 + 并发:不开线程,串行执行(同步函数霸道,执行完start,必须把同步函数的任务执行完,再执行end)。
NSLog(@"----%@",[NSThread currentThread] );
//1,创建队列
/*参数1,C语言的字符串,标签
参数2,队列类型
DISPATCH_QUEUE_CONCURRENT 并发
DISPATCH_QUEUE_SERIAL 串行
*/
// dispatch_queue_t queue = dispatch_queue_create("fff", DISPATCH_QUEUE_CONCURRENT);
//获得全局并发队列,参数1,优先级
//1,获取并发队列,
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSLog(@"start");
//2,同步函数把任务添加到队列
dispatch_sync(queue, ^{
NSLog(@"download1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3---%@",[NSThread currentThread]);
});
NSLog(@"end");
2)异步 + 并发 :开多条线程,队列任务并发执行(异步函数谦让,执行完start,可以先执行end,再执行异步函数的任务)
NSLog(@"----%@",[NSThread currentThread] );
//1,获取并发队列,
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSLog(@"start");
//2,异步函数把任务添加到队列
dispatch_async(queue, ^{
NSLog(@"download1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3---%@",[NSThread currentThread]);
});
NSLog(@"end");