热门搜索 :
考研考公
您的当前位置:首页正文

CGD基本介绍(三)

来源:东饰资讯网

GCD:Grand Central Dispatch(牛逼的中枢调度器)

GCD两个重要的概念:任务、队列

创建CGD:

  • 定制任务
  • 将任务添加到队列中

队列:

  • 并发(异步)队列(Concurrent dispatch queue):可以让多个任务并发(同时)执行,自动开启多个线程同时执行任务。并发只在异步函数下有效
  • 串行队列(Serial dispatch queue):让任务一个接着一个的执行(一个任务执行完才能执行下个任务)

GCD执行任务的常用函数:

  • 同步:只能在当前线程中执行任务,不具备开启线程的能力(同步函数会立马执行)
  • 异步:可以在新的线程中执行任务,具备开启新线程的能力

代码中获取并发队列:

// 创建并发队列
dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_CONCURRENT);
// 获取全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

代码中获取串行队列:

// 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
// 获取主队列,主队列是GCD自带的一种特殊的串行队列,主队列中的任务都会放到主线程中运行

/**
 * 异步函数 + 并发队列:可以同时开启多条线程
 */
- (void)asyncConcurrent {
    NSLog(@"asyncConcurrent--------start");
    /* 1.创建一个并发队列
     * label : 相当于队列的名字,唯一的标示
     * DISPATCH_QUEUE_CONCURRENT  并发队列;DISPATCH_QUEUE_SERIAL  串行队列
     */
    // dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_CONCURRENT);
    
    /* 1.获得全局的并发队列(GCD默认提供了全局的并发队列,供整个程序使用,不需创建)
     * dispatch_get_global_queue(long identifier, unsigned long flags)
     * long identifier:队列优先级,下面列出的优先级从高到低
     * DISPATCH_QUEUE_PRIORITY_HIGH
     * DISPATCH_QUEUE_PRIORITY_DEFAULT  (默认)
     * DISPATCH_QUEUE_PRIORITY_LOW
     * DISPATCH_QUEUE_PRIORITY_BACKGROUND
     * flags默认传0
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 2.将任务加入队列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncConcurrent--------end");
}

结果:


image
/**
 * 同步函数 + 并发队列:不会开启新的线程
 */
- (void)syncConcurrent {
    NSLog(@"syncConcurrent--------start");
    // 1.获得全局的并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 2.将任务加入队列
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"syncConcurrent--------end");
}

结果:


image
/**
 * 异步函数 + 串行队列:会开启新的线程,但是任务是串行的,执行完一个任务,再执行下一个任务
 */
- (void)asyncSerial {
    NSLog(@"asyncSerial--------start");
    // 1.创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
//    dispatch_queue_t queue = dispatch_queue_create("com.520it.queue", NULL);
    
    // 2.将任务加入队列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncSerial--------end");
}

结果:


image
/**
 * 同步函数 + 串行队列:不会开启新的线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务
 */
- (void)syncSerial {
    NSLog(@"syncSerial--------start");
    // 1.创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
    
    // 2.将任务加入队列
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"syncSerial--------end");
}

结果:


image
/**
 * 异步函数 + 主队列:只在主线程中执行任务,异步函数在主队列上不会开启线程
 */
- (void)asyncMain
{
    NSLog(@"asyncMain ----- start");
    // 1.获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 2.将任务加入队列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncMain ----- end");
}

结果:


image
/**
 * 同步函数 + 主队列:
 */
- (void)syncMain
{
    NSLog(@"syncMain ----- 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(@"syncMain ----- end");
}

结果:


image

出现这种情况的原因:主线程先执行syncMain函数,但是在syncMain函数中走到dispatch_sync(queue, ^{
NSLog(@"1-----%@", [NSThread currentThread]);
});的时候,因为这句话是同步主队列中执行,需要优先执行这句话,但是syncMain还没执行完,同样需要先执行完syncMain函数,造成冲突,所以就出现了上图的情况。

线程之间的通信
dispatch_queue_t queue = dispatch_queue_create("11111",  DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        // 进行耗时的操作呢
        dispatch_async(dispatch_get_main_queue(), ^{
            // 回到主线程进行UI刷新
    });
});
Top