因为GCD有很多好处啊,具体如下:
- GCD可用于多核的并行运算
- GCD会自动利用更多的CPU内核(比如双核、四核)
- GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程)
- 程序员只需要告诉GCD想要执行什么任务,不需要编写任何线程管理代码
- 同步执行(sync):只能在当前线程中执行任务,不具备开启新线程的能力
- 异步执行(async):可以在新的线程中执行任务,具备开启新线程的能力
- 并发队列(Concurrent Dispatch Queue):可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务)
- 并发功能只有在异步(dispatch_async)函数下才有效
- 串行队列(Serial Dispatch Queue):让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)
GCD的使用步骤其实很简单,只有两步。
- 创建一个队列(串行队列或并发队列)
- 将任务添加到队列中,然后系统就会根据任务类型执行任务(同步执行或异步执行)
并发队列+同步执行并发队列 串行队列 主队列 同步(sync) 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务 异步(async) 有开启新线程,并发执行任务 有开启新线程(1条),串行执行任务 没有开启新线程,串行执行任务 - (void) syncConcurrent { NSLog(@"syncConcurrent---begin"); dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"syncConcurrent---end"); }
-
begin 11 22 33 end 主线程中执行 一个一个执行
-
并发队列+异步执行
-
- (void) asyncConcurrent { NSLog(@"asyncConcurrent---begin"); dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"asyncConcurrent---end"); }
-
begin end 123 123 主线程 又开辟了3条线程 交替同时执行
-
串行队列 + 同步执行
-
- (void) syncSerial { NSLog(@"syncSerial---begin"); dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"syncSerial---end"); }
-
begin 11 22 33 end 主线程中执行 没开辟新的线程
-
串行队列 + 异步执行
-
- (void) asyncSerial { NSLog(@"asyncSerial---begin"); dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"asyncSerial---end"); }
-
begin end 11 22 33 开辟了一条线程
-
主队列 + 同步执行
-
- (void)syncMain { NSLog(@"syncMain---begin"); dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"syncMain---end");
-
begin 线程阻塞
-
主队列+异步执行
-
- (void)asyncMain { NSLog(@"asyncMain---begin"); dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"2------%@",[NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 2; ++i) { NSLog(@"3------%@",[NSThread currentThread]); } }); NSLog(@"asyncMain---end"); }
-
begin end 11 22 33 开辟线程的能力
-
线程间通信 主线程UI刷新(点击、滚动、拖拽等)其他线程耗时操作(图片下载、文件上传)
-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for (int i = 0; i < 2; ++i) { NSLog(@"1------%@",[NSThread currentThread]); } // 回到主线程 dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"2-------%@",[NSThread currentThread]); }); }); 11 2
-
栅栏方法 分割操作组
-
(void)barrier { dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ NSLog(@"----1-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----2-----%@", [NSThread currentThread]); }); dispatch_barrier_async(queue, ^{ NSLog(@"----barrier-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----3-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----4-----%@", [NSThread currentThread]); }); } 12 barrier 34
-
延时执行方法
-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 2秒后异步执行这里的代码... NSLog(@"run-----"); });
-
只执行一次 单例
-
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // 只执行1次的代码(这里面默认是线程安全的) });
-
快速迭代方法 可以便利多个数字
-
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(6, queue, ^(size_t index) { NSLog(@"%zd------%@",index, [NSThread currentThread]); });102435
-
队列组 两个耗时操作 执行完回到主线程
-
dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 执行1个耗时的异步操作 }); dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 执行1个耗时的异步操作 }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 等前面的异步操作都执行完毕后,回到主线程... });
- 并发队列(Concurrent Dispatch Queue):可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务)