GCD

2020-03-29  本文已影响0人  Gunks

https://www.jianshu.com/p/77c5051aede2

\color{red}{注释}: GCD的两个核心概念:任务和队列

  并行队列:
         可以让多个任务并发执行,以提高执行效率
         并发功能仅在异步(dispatch_async)函数下才有效
  串行队列:
         在当前线程中让任务一个接着一个地执行

1.创建队列

// 队列类型
dispatch_queue_t
// 第一个参数:队列名称  第二个参数:队列类型
dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
  // 队列类型
  // 串行队列标识:本质就是NULL,但建议不要写成NULL,可读性不好
  DISPATCH_QUEUE_SERIAL
  // 并行队列标识
  DISPATCH_QUEUE_CONCURRENT
1.1 创建并行队列的两种方式
1.2 创建串行队列的两种方式
  // 创建串行队列(队列类型传递DISPATCH_QUEUE_SERIAL或者NULL)
  dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_SERIAL);
  // 主队列中的任务,都会放到主线程中执行
  dispatch_queue_t queue = dispatch_get_main_queue();

—————————————————————————————————————————————

2.同步(sync)函数 \color{red}{和} 异步(async)函数


2.1 同步函数:不具备开启新线程的能力,只能在当前线程中执行任务
// queue:队列
// block:任务
dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
2.2异步函数:具备开启线程的能力,但不一定开启新线程,比如:当前队列为主队列,异步函数也不会开启新的线程
// queue:队列
// block:任务
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

\color{red}{\Large{经验总结}}

—————————————————————————————————————————————

3.程序猿只需要做下列事情

base.png

—————————————————————————————————————————————

4.实战

- (void)asyncConcurrent{
    // 1.创建并行队列
//    dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 2.通过异步函数将将任务加入队列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    // 证明:异步函数添加任务到队列中,任务【不会】立即执行
    NSLog(@"asyncConcurrent--------end");

    // 释放队列,ARC中无需也不允许调用这个方法
//    dispatch_release(queue);
}
- (void)asyncSerial{
    // 1.创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_SERIAL);
//    dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", NULL);
    // 2.通过异步函数将任务加入队列
    dispatch_async(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
    // 证明:异步函数添加任务到队列中,任务【不会】立马执行
    NSLog(@"asyncConcurrent--------end");
}
- (void)asyncMain{
    // 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]);
    });
}
- (void)syncConcurrent{
    // 1.获得全局的并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 2.通过同步函数将任务加入队列
    dispatch_sync(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
    // 证明:同步函数添加任务到队列中,任务【立马执行】
    NSLog(@"syncConcurrent--------end");
}

\color{red}{注:易发生死锁}

- (void)syncMain{
    // 1.创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_SERIAL);
    // 2.将任务加入队列
    dispatch_sync(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
}
dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
       NSLog(@"1-----%@", [NSThread currentThread]);
       // 这里阻塞了
       dispatch_sync(queue, ^{
             NSLog(@"2-----%@", [NSThread currentThread]);
       });
});
  - (void)syncMain  {
      // 获得主队列
      dispatch_queue_t queue = dispatch_get_main_queue();
      // 这里阻塞了
      dispatch_sync(queue, ^{
          NSLog(@"1-----%@", [NSThread currentThread]);
      });
      dispatch_sync(queue, ^{
          NSLog(@"2-----%@", [NSThread currentThread]);
      });
      dispatch_sync(queue, ^{
          NSLog(@"3-----%@", [NSThread currentThread]);
      });
  }

—————————————————————————————————————————————

5.GCD实现线程间通信

上一篇下一篇

猜你喜欢

热点阅读