GCD初级篇(一)

1. 队列创建

/**
 队列创建
 dispatch_queue_create()
 第一个参数表示队列的唯一标识符,用于 DEBUG,可为空
 第二个参数 DISPATCH_QUEUE_SERIAL表示串行队列,DISPATCH_QUEUE_CONCURRENT表示并发队列。
 */
- (void)createQueue{
     // 串行队列创建
    dispatch_queue_t serialQueue = dispatch_queue_create("serial.kelly.com", DISPATCH_QUEUE_SERIAL);
    // 并发队列创建
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
    // 获取主队列:所有放在主队列中的任务,都会放到主线程中执行。
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // 对于并发队列,GCD 默认提供了全局并发队列(Global Dispatch Queue)
    // 第一个参数优先级,第二个填0即可
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}

2. 线程组合运用


2.1 同步执行 + 并发队列
特点:在当前线程中执行任务,不会开启新线程,执行完一个任务,再执行下一个任务。

- (void)syncAndConcurrent{
    NSLog(@"当前线程= %@",[NSThread currentThread]);
    NSLog(@"syncAndConcurrent begin ---");
    dispatch_queue_t queue = dispatch_queue_create("syncAndConcurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        // 模拟耗时2s
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndConcurrent --> 1 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        // 模拟耗时2s
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndConcurrent --> 2 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        // 模拟耗时2s
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndConcurrent --> 3 当前线程=%@",[NSThread currentThread]);
    });
    NSLog(@"syncAndConcurrent end ---");
}
1533817936498.jpg

2.2 异步执行 + 并发队列
特点:可以开启多个线程,任务交替(同时)执行。

- (void)asyncAndConcurrent{
    NSLog(@"当前线程= %@",[NSThread currentThread]);
    NSLog(@"asyncAndConcurrent begin ---");
    dispatch_queue_t queue = dispatch_queue_create("asyncAndConcurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndConcurrent --> 1 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndConcurrent --> 2 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndConcurrent --> 3 当前线程=%@",[NSThread currentThread]);
    });
    NSLog(@"asyncAndConcurrent end ---");
}
1533818173721.jpg

2.3 同步执行 + 串行队列
特点:不会开启新线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务。

- (void)syncAndSerial{
    NSLog(@"当前线程= %@",[NSThread currentThread]);
    NSLog(@"syncAndSerial begin ---");
    dispatch_queue_t queue = dispatch_queue_create("syncAndSerial.kelly.com", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndSerial --> 1 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndSerial --> 2 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndSerial --> 3 当前线程=%@",[NSThread currentThread]);
    });
    NSLog(@"syncAndSerial end ---");
}
1533818397023.jpg

2.4 异步执行 + 串行队列
会开启新线程,但是因为任务是串行的,执行完一个任务,再执行下一个任务。

- (void)asyncAndSerial{
    NSLog(@"当前线程= %@",[NSThread currentThread]);
    NSLog(@"asyncAndSerial begin ---");
    dispatch_queue_t queue = dispatch_queue_create("asyncAndSerial.kelly.com", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndSerial --> 1 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndSerial --> 2 当前线程=%@",[NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"asyncAndSerial --> 3 当前线程=%@",[NSThread currentThread]);
    });
    NSLog(@"asyncAndSerial end ---");
}
1533818515599.jpg

2.5 异步执行 + 主队列
特点:只在主线程中执行任务,执行完一个任务,再执行下一个任务

- (void)asyncAndMain{
    NSLog(@"当前线程---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"asyncAndMain---begin");
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndMain --> 1 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndMain --> 2 当前线程=%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"syncAndMain --> 3 当前线程=%@",[NSThread currentThread]);
    });
    
    NSLog(@"asyncAndMain---end");
}
1533818828690.jpg

2.6 线程间通信

- (void)communication{
    // 获取全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 获取主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // 异步追加任务
    dispatch_async(queue, ^{
       
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdCommunication --> 1 当前线程=%@",[NSThread currentThread]);
        // 回到主线程
        dispatch_async(mainQueue, ^{
            // 刷新UI等操作
            [NSThread sleepForTimeInterval:2];
            NSLog(@"gcdCommunication --> 2 当前线程=%@",[NSThread currentThread]);
        });
        
    });
}

3. 多线程其他方法

3.1 栅栏方法 dispatch_barrier_async
dispatch_barrier_async函数会等待前边追加到并发队列中的任务全部执行完毕之后,再将指定的任务追加到该异步队列中。然后在dispatch_barrier_async函数追加的任务执行完毕之后,异步队列才恢复为一般动作,接着追加任务到该异步队列并开始执行。

- (void)gcdAndBarrier{
    dispatch_queue_t queue = dispatch_queue_create("gcdAndBarrier.kelly.com", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"gcdAndBarrier start");
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdAndBarrier -- > 1 当前线程%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdAndBarrier -- > 2 当前线程%@",[NSThread currentThread]);
    });
    
    // 追加barrier
    dispatch_barrier_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdAndBarrier -- > 3 当前线程%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdAndBarrier -- > 4 当前线程%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"gcdAndBarrier -- > 5 当前线程%@",[NSThread currentThread]);
    });
    
}
1533819192522.jpg

3.2 GCD 延时

- (void)gcdAndDelay{
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"gcdAndDelay---begin");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
        // 2.0秒后异步追加任务代码到主队列,并开始执行
        NSLog(@"after---%@",[NSThread currentThread]);
    });
    NSLog(@"gcdAndDelay---end");
}

3.3 队列组 dispatch_group_notify

- (void)groupNotify {
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"group---begin");
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"groupNotify 1---%@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"groupNotify 2---%@",[NSThread currentThread]);
    });
    
    // 等前面的异步任务1、任务2都执行完毕后,回到主线程执行下边任务
    dispatch_group_notify(group, mainQueue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"groupNotify 3---%@",[NSThread currentThread]);
        NSLog(@"group---end");
    });
}

3.4 dispatch_group_wait
暂停当前线程(阻塞当前线程),等待指定的 group 中的任务执行完成后,才会往下继续执行。

- (void)groupWait {
    NSLog(@"groupWait---%@",[NSThread currentThread]);
    NSLog(@"group---begin");
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"groupWait 1---%@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"groupWait 2---%@",[NSThread currentThread]);
    });
    // 等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程)
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"group---end");
}
1533819498842.jpg

今天暂时写这么多,有什么不足的地方,希望各位指出来大家一起学习。

你可能感兴趣的:(GCD初级篇(一))