/**
* async -- 并发队列
* 会创建线程,一般同时开多条
* 并发执行任务
*/
<span style="font-size:14px;">- (void)asyncGlobalQueue { // 获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 添加 全局队列 中去 异步 执行 dispatch_async(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); } </span>
<span style="font-size:14px;">-----下载图片4---<NSThread: 0x7fbd52c9b390>{number = 5, name = (null)} -----下载图片3---<NSThread: 0x7fbd52f00230>{number = 4, name = (null)} -----下载图片2---<NSThread: 0x7fbd52e3eed0>{number = 3, name = (null)} -----下载图片5---<NSThread: 0x7fbd52e2eb40>{number = 6, name = (null)} -----下载图片1---<NSThread: 0x7fbd52c06010>{number = 2, name = (null)}</span>
/**
* async -- 串行队列
* 会创建线程, 一般只开1条线程
* 串行执行任务(一个任务执行完毕后再执行下一个任务)
*/
- (void)asyncSerialQueue { // 1.创建一个串行队列 dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 2.将任务添加到串行队列中 异步 执行 dispatch_async(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); }
-----下载图片1---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)} -----下载图片2---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)} -----下载图片3---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)} -----下载图片4---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)} -----下载图片5---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
/**
* async -- 主队列
*/
- (void)asyncMainQueue { // 1.主队列(添加到主队列中的任务,都会自动放到主线程中去执行) dispatch_queue_t queue = dispatch_get_main_queue(); // 2.添加 任务 到主队列中 异步 执行 dispatch_async(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); }
-----下载图片1---<NSThread: 0x7faaca905000>{number = 1, name = main} -----下载图片2---<NSThread: 0x7faaca905000>{number = 1, name = main} -----下载图片3---<NSThread: 0x7faaca905000>{number = 1, name = main} -----下载图片4---<NSThread: 0x7faaca905000>{number = 1, name = main} -----下载图片5---<NSThread: 0x7faaca905000>{number = 1, name = main}
/**
* sync -- 主队列(会卡死)
*/
- (void)syncMainQueue { NSLog(@"syncMainQueue----begin--"); // 1.主队列(添加到主队列中的任务,都会自动放到主线程中去执行) dispatch_queue_t queue = dispatch_get_main_queue(); // 2.添加 任务 到主队列中 异步 执行 dispatch_sync(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); NSLog(@"syncMainQueue----end--"); }
syncMainQueue----begin--
/**
* sync -- 并发队列
* 不会创建线程
* 串行执行任务(一个任务执行完毕后再执行下一个任务)
* 并发队列失去了并发的功能
*/
- (void)syncGlobalQueue { // 获得全局的并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 添加到 全局并发队列 中 同步 执行 dispatch_sync(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); }
-----下载图片1---<NSThread: 0x7fe5b2904c30>{number = 1, name = main} -----下载图片2---<NSThread: 0x7fe5b2904c30>{number = 1, name = main} -----下载图片3---<NSThread: 0x7fe5b2904c30>{number = 1, name = main} -----下载图片4---<NSThread: 0x7fe5b2904c30>{number = 1, name = main} -----下载图片5---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
/**
* sync -- 串行队列
* 不会创建线程
* 串行执行任务(一个任务执行完毕后再执行下一个任务)
*/
- (void)syncSerialQueue { // 创建一个串行队列 dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 将 任务 添加到 串行队列 中 同步 执行 dispatch_sync(queue, ^{ NSLog(@"-----下载图片1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片3---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片4---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"-----下载图片5---%@", [NSThread currentThread]); }); }
-----下载图片1---<NSThread: 0x7fdb40f05020>{number = 1, name = main} -----下载图片2---<NSThread: 0x7fdb40f05020>{number = 1, name = main} -----下载图片3---<NSThread: 0x7fdb40f05020>{number = 1, name = main} -----下载图片4---<NSThread: 0x7fdb40f05020>{number = 1, name = main} -----下载图片5---<NSThread: 0x7fdb40f05020>{number = 1, name = main}