iOS多线程之GCD-同步、异步、并发、串行、线程组、栅栏函数、信号量等全网最全的总结

简介: iOS多线程之GCD-同步、异步、并发、串行、线程组、栅栏函数、信号量等全网最全的总结

1. GCD简介

GCD全称:Grand Central Dispatch,译为大型的中枢调度器、纯C语言实现,提供了非常多强大的功能;

优势:旨在替代NSThread等线程技术,充分利用设备的多核。


2. GCD队列

队列(Dispatch Queue)

这里的队列指执行任务的等待队列,即用来存放任务的队列。队列是一种特殊的线性表,采用 FIFO(先进先出)的原则,即新任务总是被插入到队列的末尾,而读取任务的时候总是从队列的头部开始读取。每读取一个任务,则从队列中释放一个任务。


GCD的队列可以分为2大类型:

并发队列(Concurrent Dispatch Queue)

1)可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务)

2)并发功能只有在异步(dispatch_async)函数下才有效


串行队列(Serial Dispatch Queue)

让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)


3. 同步、异步、并发、串行

同步和异步主要影响: 能不能开启新的线程

同步: 在当前线程中执行任务,不具备开启新线程的能力

异步: 在新的线程中执行任务,具备开启新线程的能力


并发和串行主要影响: 任务的执行方式

并发: 多个任务并发(同时)执行

串行: 一个任务执行完毕后,再执行下一个任务


4. 创建队列

可以使用 dispatch_queue_create 方法来创建队列。该方法需要传入两个参数:

第一个参数表示队列的唯一标识符

第二个参数 DISPATCH_QUEUE_SERIAL或NULL: 表示串行队列 DISPATCH_QUEUE_CONCURRENT: 表示并发队列


系统默认提供了主队列,主队列也属于一个串行队列,可通过dispatch_get_main_queue获取主队列,放在主队列中的任务都会放在主线程中执行,系统还提供了一个并发队列,dispatch_get_global_queue() 方法获得全局并发队列。

- (void)gcdCreateQueue {
    dispatch_queue_t queue1 = dispatch_queue_create("com.glt.test.queue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue2 = dispatch_queue_create("com.glt.test.queue2", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue3 = dispatch_queue_create("com.glt.test.queue3", NULL);
    dispatch_queue_t queue4 = dispatch_get_main_queue();
    dispatch_queue_t queue5 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"%@", queue1);
    NSLog(@"%@", queue2);
    NSLog(@"%@", queue3);
    NSLog(@"%@", queue4);
    NSLog(@"%@", queue5);
}
- (void)gcdCreateQueue {
    dispatch_queue_t queue1 = dispatch_queue_create("com.glt.test.queue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue2 = dispatch_queue_create("com.glt.test.queue2", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue3 = dispatch_queue_create("com.glt.test.queue3", NULL);
    dispatch_queue_t queue4 = dispatch_get_main_queue();
    dispatch_queue_t queue5 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"%@", queue1);
    NSLog(@"%@", queue2);
    NSLog(@"%@", queue3);
    NSLog(@"%@", queue4);
    NSLog(@"%@", queue5);
}

运行结果:

2021-04-10 10:51:46.206755+0800 GCD[1482:45167] <OS_dispatch_queue_concurrent: com.glt.test.queue1>
2021-04-10 10:51:46.206850+0800 GCD[1482:45167] <OS_dispatch_queue_serial: com.glt.test.queue2>
2021-04-10 10:51:46.206930+0800 GCD[1482:45167] <OS_dispatch_queue_serial: com.glt.test.queue3>
2021-04-10 10:51:46.207000+0800 GCD[1482:45167] <OS_dispatch_queue_main: com.apple.main-thread>
2021-04-10 10:51:46.207061+0800 GCD[1482:45167] <OS_dispatch_queue_global: com.apple.root.default-qos>

5. 创建任务

dispatch_sync创建同步任务,dispatch_async创建异步线程任务

- (void)gcdCreateTask {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test.queue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"同步任务队列");
    });
    dispatch_async(queue, ^{
        NSLog(@"异步任务队列");
    });
}
- (void)gcdCreateTask {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test.queue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"同步任务队列");
    });
    dispatch_async(queue, ^{
        NSLog(@"异步任务队列");
    });
}


6. 队列组合

不同的队列组合产生的效果和影响不同,具体总结如下:

接下来通过样例来分别看一下组合效果(文末附源码工程链接):

1)同步+串行

不会开启新的线程、把任务添加到当前线程中、任务一个接一个的执行


- (void)syncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}- (void)syncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 10:57:25.460191+0800 GCD[1540:51156] 任务1
2021-04-10 10:57:25.460303+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}
2021-04-10 10:57:27.460477+0800 GCD[1540:51156] 任务2
2021-04-10 10:57:27.460732+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}
2021-04-10 10:57:27.460895+0800 GCD[1540:51156] 任务3
2021-04-10 10:57:27.461054+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}
2021-04-10 10:57:25.460191+0800 GCD[1540:51156] 任务1
2021-04-10 10:57:25.460303+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}
2021-04-10 10:57:27.460477+0800 GCD[1540:51156] 任务2
2021-04-10 10:57:27.460732+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}
2021-04-10 10:57:27.460895+0800 GCD[1540:51156] 任务3
2021-04-10 10:57:27.461054+0800 GCD[1540:51156] 当前线程: <NSThread: 0x60000294c180>{number = 1, name = main}

2)同步+并发

不会开启新的线程、把任务添加到当前线程中、任务一个接一个的执行、并发队列在异步任务中才有效



- (void)syncConcurrent {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}
- (void)syncConcurrent {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:00:59.308934+0800 GCD[1570:54003] 任务1
2021-04-10 11:00:59.309035+0800 GCD[1570:54003] 当前线程: <NSThread: 0x6000031b81c0>{number = 1, name = main}
2021-04-10 11:01:01.309702+0800 GCD[1570:54003] 任务2
2021-04-10 11:01:01.309948+0800 GCD[1570:54003] 当前线程: <NSThread: 0x6000031b81c0>{number = 1, name = main}
2021-04-10 11:01:01.310101+0800 GCD[1570:54003] 任务3
2021-04-10 11:01:01.310262+0800 GCD[1570:54003] 当前线程: <NSThread: 0x6000031b81c0>{number = 1, name = main}

3)异步+串行

会开启新的线程、把任务添加到当前线程中、任务一个接一个的执行


- (void)asyncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}
- (void)asyncSerial {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:04:22.612780+0800 GCD[1612:57386] 任务1
2021-04-10 11:04:22.612881+0800 GCD[1612:57386] 当前线程: <NSThread: 0x6000009a2a40>{number = 5, name = (null)}
2021-04-10 11:04:24.615244+0800 GCD[1612:57386] 任务2
2021-04-10 11:04:24.615514+0800 GCD[1612:57386] 当前线程: <NSThread: 0x6000009a2a40>{number = 5, name = (null)}
2021-04-10 11:04:24.615686+0800 GCD[1612:57386] 任务3
2021-04-10 11:04:24.615854+0800 GCD[1612:57386] 当前线程: <NSThread: 0x6000009a2a40>{number = 5, name = (null)}

4)异步+并发

会开启新的线程、把任务添加到新的线程中、并发的执行任务


- (void)asyncConcurrent {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:09:06.322720+0800 GCD[1664:61535] 任务1 - <NSThread: 0x6000002d1700>{number = 6, name = (null)}
2021-04-10 11:09:06.322726+0800 GCD[1664:61536] 任务3 - <NSThread: 0x6000002c5e80>{number = 7, name = (null)}
2021-04-10 11:09:08.326379+0800 GCD[1664:61541] 任务2 - <NSThread: 0x60000029a840>{number = 4, name = (null)}2021-04-10 11:09:06.322720+0800 GCD[1664:61535] 任务1 - <NSThread: 0x6000002d1700>{number = 6, name = (null)}
2021-04-10 11:09:06.322726+0800 GCD[1664:61536] 任务3 - <NSThread: 0x6000002c5e80>{number = 7, name = (null)}
2021-04-10 11:09:08.326379+0800 GCD[1664:61541] 任务2 - <NSThread: 0x60000029a840>{number = 4, name = (null)}

5)同步+主队列

不会开启新的线程、把任务添加到当前线程中、产生死锁(2和3相互等待产生死锁)


- (void)syncMain {
//    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);//不会产生死锁
    dispatch_queue_t queue = dispatch_get_main_queue();//产生死锁
    NSLog(@"1");
    dispatch_sync(queue, ^{
        NSLog(@"2");
    });
    NSLog(@"3");
}
- (void)syncMain {
//    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_SERIAL);//不会产生死锁
    dispatch_queue_t queue = dispatch_get_main_queue();//产生死锁
    NSLog(@"1");
    dispatch_sync(queue, ^{
        NSLog(@"2");
    });
    NSLog(@"3");
}

运行结果:


2021-04-10 11:11:30.578822+0800 GCD[1694:64014] 1

6)异步+主队列

不会开启新的线程、把任务添加到当前线程中、任务一个接一个的执行

- (void)asyncMain {
    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
    });
    NSLog(@"3");
}

运行结果:

2021-04-10 11:24:34.478703+0800 GCD[1755:73277] 1
2021-04-10 11:24:34.478825+0800 GCD[1755:73277] 3
2021-04-10 11:24:34.481464+0800 GCD[1755:73277] 2
2021-04-10 11:24:34.478703+0800 GCD[1755:73277] 1
2021-04-10 11:24:34.478825+0800 GCD[1755:73277] 3
2021-04-10 11:24:34.481464+0800 GCD[1755:73277] 2

7)同步+全局队列

会开启新的线程、把任务添加到当前线程中、没有开启新的线程、任务一个接一个的执行

- (void)syncGlobal {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}
- (void)syncGlobal {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(queue, ^{
        NSLog(@"任务1");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        sleep(2);
        NSLog(@"任务2");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3");
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:26:45.632522+0800 GCD[1778:75212] 任务1
2021-04-10 11:26:45.632626+0800 GCD[1778:75212] 当前线程: <NSThread: 0x600003910900>{number = 1, name = main}
2021-04-10 11:26:47.633010+0800 GCD[1778:75212] 任务2
2021-04-10 11:26:47.633255+0800 GCD[1778:75212] 当前线程: <NSThread: 0x600003910900>{number = 1, name = main}
2021-04-10 11:26:47.633437+0800 GCD[1778:75212] 任务3
2021-04-10 11:26:47.633594+0800 GCD[1778:75212] 当前线程: <NSThread: 0x600003910900>{number = 1, name = main}

8)异步+全局队列

会开启新的线程、把任务添加到新的线程中、并发的执行任务

- (void)asyncGlobal {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
    });
}
- (void)asyncGlobal {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:30:37.836797+0800 GCD[1814:79285] 任务1 - <NSThread: 0x6000032fac40>{number = 4, name = (null)}
2021-04-10 11:30:37.836805+0800 GCD[1814:79287] 任务3 - <NSThread: 0x6000032a96c0>{number = 6, name = (null)}
2021-04-10 11:30:39.836879+0800 GCD[1814:79283] 任务2 - <NSThread: 0x6000032a9080>{number = 7, name = (null)}

9)串行+异步+同步

死锁


- (void)concurrentSyncAsync {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test.queue1", DISPATCH_QUEUE_SERIAL);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"4");
        dispatch_sync(queue, ^{
            NSLog(@"2");
        });
        NSLog(@"3");
    });
    NSLog(@"5");
}

运行结果:

2021-04-10 11:33:34.234259+0800 GCD[1834:81537] 1
2021-04-10 11:33:34.234365+0800 GCD[1834:81537] 5
2021-04-10 11:33:34.234377+0800 GCD[1834:81712] 4
2021-04-10 11:33:34.234259+0800 GCD[1834:81537] 1
2021-04-10 11:33:34.234365+0800 GCD[1834:81537] 5
2021-04-10 11:33:34.234377+0800 GCD[1834:81712] 4


7. 线程间通信

子线程执行耗时操作(图片下载、图片合成、文件下载等),回到主线程刷新UI

- (void)gcdMessage {
    NSLog(@"1");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"2-刷新UI");
        });
    });
    NSLog(@"3");
}
- (void)gcdMessage {
    NSLog(@"1");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"2-刷新UI");
        });
    });
    NSLog(@"3");
}

运行结果:

2021-04-10 11:39:23.991812+0800 GCD[1865:85789] 1
2021-04-10 11:39:23.991908+0800 GCD[1865:85789] 3
2021-04-10 11:39:25.992573+0800 GCD[1865:85789] 2-刷新UI2021-04-10 11:39:23.991812+0800 GCD[1865:85789] 1
2021-04-10 11:39:23.991908+0800 GCD[1865:85789] 3
2021-04-10 11:39:25.992573+0800 GCD[1865:85789] 2-刷新UI

8. 栅栏函数

栅栏函数:dispatch_barrier_async会等待前边追加到并发队列中的任务全部执行完毕之后,再将指定的任务追加到该异步队列中;并且栅栏函数只能使用自己创建的并发队列、全局队列没有效果(使用全局队列相当于普通的异步任务),如果是主队列则依次执行;

dispatch_barrier_sync与dispatch_barrier_sync区别是:dispatch_barrier_sync内部的函数是在主线程中执行,dispatch_barrier_sync执行的任务是在新开辟的子线程中执行;

- (void)barrierTask {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);//栅栏函数有效果
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//栅栏函数无效果
//    dispatch_queue_t queue = dispatch_get_main_queue();//栅栏函数有效果、队列依次执行
    dispatch_async(queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2 - %@", [NSThread currentThread]);
    });
    dispatch_barrier_sync(queue, ^{
        sleep(4);
        NSLog(@"栅栏任务 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务4 - %@", [NSThread currentThread]);
    });
}
- (void)barrierTask {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);//栅栏函数有效果
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//栅栏函数无效果
//    dispatch_queue_t queue = dispatch_get_main_queue();//栅栏函数有效果、队列依次执行
    dispatch_async(queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"任务2 - %@", [NSThread currentThread]);
    });
    dispatch_barrier_sync(queue, ^{
        sleep(4);
        NSLog(@"栅栏任务 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务4 - %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 11:44:44.907322+0800 GCD[1923:90606] 任务1 - <NSThread: 0x600000f3cdc0>{number = 6, name = (null)}
2021-04-10 11:44:46.911944+0800 GCD[1923:90607] 任务2 - <NSThread: 0x600000f2e2c0>{number = 5, name = (null)}
2021-04-10 11:44:50.913348+0800 GCD[1923:90420] 栅栏任务 - <NSThread: 0x600000f60900>{number = 1, name = main}
2021-04-10 11:44:50.913659+0800 GCD[1923:90609] 任务4 - <NSThread: 0x600000f64fc0>{number = 4, name = (null)}
2021-04-10 11:44:50.913642+0800 GCD[1923:90607] 任务3 - <NSThread: 0x600000f2e2c0>{number = 5, name = (null)}


9. 延时执行任务

- (void)gcdAfter {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延时执行任务");
    });
}
- (void)gcdAfter {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延时执行任务");
    });
}


10. dispatch_once_t

仅执行1次,多线程环境下保证线程安全,注意:如果将onceToken设置为0,下次再调用依旧会执行,常用于销毁单例

- (void)gcdOnce {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"only one");
    });
}


11. dispatch_apply

按照指定的次数将指定的任务追加到指定的队列中,并等待全部队列执行结束,无论是在串行队列,还是并发队列中,dispatch_apply 都会等待全部任务执行完毕


- (void)gcdApply {
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"begain");
    dispatch_apply(6, queue, ^(size_t time) {
        NSLog(@"%zu - %@", time, [NSThread currentThread]);
    });
    NSLog(@"end");
}

运行结果:


2021-04-10 11:56:20.440608+0800 GCD[1959:97881] begain
2021-04-10 11:56:20.440731+0800 GCD[1959:97881] 0 - <NSThread: 0x600002b94a00>{number = 1, name = main}
2021-04-10 11:56:20.440806+0800 GCD[1959:98094] 1 - <NSThread: 0x600002bd5680>{number = 3, name = (null)}
2021-04-10 11:56:20.440815+0800 GCD[1959:97881] 3 - <NSThread: 0x600002b94a00>{number = 1, name = main}
2021-04-10 11:56:20.440816+0800 GCD[1959:98090] 2 - <NSThread: 0x600002b99980>{number = 6, name = (null)}
2021-04-10 11:56:20.440839+0800 GCD[1959:98093] 4 - <NSThread: 0x600002bd5980>{number = 5, name = (null)}
2021-04-10 11:56:20.440837+0800 GCD[1959:98089] 5 - <NSThread: 0x600002bcc880>{number = 4, name = (null)}
2021-04-10 11:56:20.440918+0800 GCD[1959:97881] end
2021-04-10 11:56:20.440608+0800 GCD[1959:97881] begain
2021-04-10 11:56:20.440731+0800 GCD[1959:97881] 0 - <NSThread: 0x600002b94a00>{number = 1, name = main}
2021-04-10 11:56:20.440806+0800 GCD[1959:98094] 1 - <NSThread: 0x600002bd5680>{number = 3, name = (null)}
2021-04-10 11:56:20.440815+0800 GCD[1959:97881] 3 - <NSThread: 0x600002b94a00>{number = 1, name = main}
2021-04-10 11:56:20.440816+0800 GCD[1959:98090] 2 - <NSThread: 0x600002b99980>{number = 6, name = (null)}
2021-04-10 11:56:20.440839+0800 GCD[1959:98093] 4 - <NSThread: 0x600002bd5980>{number = 5, name = (null)}
2021-04-10 11:56:20.440837+0800 GCD[1959:98089] 5 - <NSThread: 0x600002bcc880>{number = 4, name = (null)}
2021-04-10 11:56:20.440918+0800 GCD[1959:97881] end


12. 线程组

1)dispatch_group_async: 先把任务放到队列中,然后将队列放入队列组中

2)dispatch_group_notify: 监听dispatch_group_async中任务的完成状态,当所有的任务都执行完成后,最后执行dispatch_group_notify内部的任务

3)dispatch_group_wait: 阻塞当前任务,会先执行等待之前的任务完成后,再执行等待后的任务

4)dispatch_group_enter会将任务数+1,dispatch_group_leave会将任务数-1,当任务数为0的时候再执行dispatch_group_notify中的任务

场景描述:我们要等前3个任务完成以后再执行其他的任务,并且这3个任务内部又有回到其他线程的操作


- (void)gcdGroup {
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
        dispatch_group_leave(group);
    });
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"任务2 - %@", [NSThread currentThread]);
            dispatch_group_leave(group);
        });
    });
//    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
        dispatch_group_leave(group);
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"任务执行完毕 - %@", [NSThread currentThread]);
    });
}
- (void)gcdGroup {
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务1 - %@", [NSThread currentThread]);
        dispatch_group_leave(group);
    });
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"任务2 - %@", [NSThread currentThread]);
            dispatch_group_leave(group);
        });
    });
//    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务3 - %@", [NSThread currentThread]);
        dispatch_group_leave(group);
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"任务执行完毕 - %@", [NSThread currentThread]);
    });
}

运行结果:

2021-04-10 12:00:52.647107+0800 GCD[1989:101516] 任务3 - <NSThread: 0x60000097e9c0>{number = 4, name = (null)}
2021-04-10 12:00:52.647116+0800 GCD[1989:101520] 任务1 - <NSThread: 0x600000940f40>{number = 6, name = (null)}
2021-04-10 12:00:55.647281+0800 GCD[1989:101346] 任务2 - <NSThread: 0x6000009501c0>{number = 1, name = main}
2021-04-10 12:00:55.647530+0800 GCD[1989:101516] 任务执行完毕 - <NSThread: 0x60000097e9c0>{number = 4, name = (null)}
2021-04-10 12:00:52.647107+0800 GCD[1989:101516] 任务3 - <NSThread: 0x60000097e9c0>{number = 4, name = (null)}
2021-04-10 12:00:52.647116+0800 GCD[1989:101520] 任务1 - <NSThread: 0x600000940f40>{number = 6, name = (null)}
2021-04-10 12:00:55.647281+0800 GCD[1989:101346] 任务2 - <NSThread: 0x6000009501c0>{number = 1, name = main}
2021-04-10 12:00:55.647530+0800 GCD[1989:101516] 任务执行完毕 - <NSThread: 0x60000097e9c0>{number = 4, name = (null)}

13. 信号量

dispatch_semaphore_create:创建一个Semaphore并设置信号的总量;

dispatch_semaphore_signal:发送信号,让信号+1;

dispatch_semaphore_wait:可以使总信号量-1,信号总量小于0时就会等待,阻塞所在线程;

dispatch_semaphore作用:

1)保持线程同步,将异步执行任务转换为同步执行任务

2保证线程安全,为线程加锁


- (void)gcdSemaphoreSync {
    NSInteger ret = [self gcdRet];
    NSLog(@"异步获取函数返回值 - %ld", ret);
}
- (NSInteger)gcdRet {
    __block NSInteger count = 0;
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), queue, ^{//模拟异步网络请求
        count = 999;
        dispatch_semaphore_signal(semaphore);//+1后为0则继续向下执行
    });
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//-1后小于0开始阻塞等待
    return count;
}
//保证线程安全,为线程加锁
- (void)gcdSemaphoreLock {
    if (!_semaphore) {
        _semaphore = dispatch_semaphore_create(1);
    }
    dispatch_queue_t queue = dispatch_queue_create("com.glt.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(self->_semaphore, DISPATCH_TIME_FOREVER);//-1后等于0开始向下进行,之后再执行到这里此处会小于0,产生阻塞,会等待解锁后继续向下执行
        NSLog(@"线程锁-begain");
        sleep(3);
        NSLog(@"线程锁-end");
        dispatch_semaphore_signal(self->_semaphore);//+1后为0,解锁,则继续执行
    });
}

方法调用:


  [self gcdSemaphoreSync];
    [self gcdSemaphoreLock];
    [self gcdSemaphoreLock];
    [self gcdSemaphoreLock];  [self gcdSemaphoreSync];
    [self gcdSemaphoreLock];
    [self gcdSemaphoreLock];
    [self gcdSemaphoreLock];

运行结果:


2021-04-10 12:04:02.670569+0800 GCD[2009:103890] 异步获取函数返回值 - 999
2021-04-10 12:04:02.670715+0800 GCD[2009:104065] 线程锁-begain
2021-04-10 12:04:05.671743+0800 GCD[2009:104065] 线程锁-end
2021-04-10 12:04:05.672013+0800 GCD[2009:104062] 线程锁-begain
2021-04-10 12:04:08.677301+0800 GCD[2009:104062] 线程锁-end
2021-04-10 12:04:08.677575+0800 GCD[2009:104064] 线程锁-begain
2021-04-10 12:04:11.678193+0800 GCD[2009:104064] 线程锁-end
2021-04-10 12:04:02.670569+0800 GCD[2009:103890] 异步获取函数返回值 - 999
2021-04-10 12:04:02.670715+0800 GCD[2009:104065] 线程锁-begain
2021-04-10 12:04:05.671743+0800 GCD[2009:104065] 线程锁-end
2021-04-10 12:04:05.672013+0800 GCD[2009:104062] 线程锁-begain
2021-04-10 12:04:08.677301+0800 GCD[2009:104062] 线程锁-end
2021-04-10 12:04:08.677575+0800 GCD[2009:104064] 线程锁-begain
2021-04-10 12:04:11.678193+0800 GCD[2009:104064] 线程锁-end

源码:https://github.com/gltwy/public

相关文章
|
2月前
|
编解码 数据安全/隐私保护 计算机视觉
Opencv学习笔记(十):同步和异步(多线程)操作打开海康摄像头
如何使用OpenCV进行同步和异步操作来打开海康摄像头,并提供了相关的代码示例。
92 1
Opencv学习笔记(十):同步和异步(多线程)操作打开海康摄像头
|
2月前
|
存储 消息中间件 资源调度
C++ 多线程之初识多线程
这篇文章介绍了C++多线程的基本概念,包括进程和线程的定义、并发的实现方式,以及如何在C++中创建和管理线程,包括使用`std::thread`库、线程的join和detach方法,并通过示例代码展示了如何创建和使用多线程。
48 1
C++ 多线程之初识多线程
|
2月前
|
Java 开发者
在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口
【10月更文挑战第20天】在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口。本文揭示了这两种方式的微妙差异和潜在陷阱,帮助你更好地理解和选择适合项目需求的线程创建方式。
20 3
|
2月前
|
Java 开发者
在Java多线程编程中,选择合适的线程创建方法至关重要
【10月更文挑战第20天】在Java多线程编程中,选择合适的线程创建方法至关重要。本文通过案例分析,探讨了继承Thread类和实现Runnable接口两种方法的优缺点及适用场景,帮助开发者做出明智的选择。
19 2
|
2月前
|
Java
Java中多线程编程的基本概念和创建线程的两种主要方式:继承Thread类和实现Runnable接口
【10月更文挑战第20天】《JAVA多线程深度解析:线程的创建之路》介绍了Java中多线程编程的基本概念和创建线程的两种主要方式:继承Thread类和实现Runnable接口。文章详细讲解了每种方式的实现方法、优缺点及适用场景,帮助读者更好地理解和掌握多线程编程技术,为复杂任务的高效处理奠定基础。
31 2
|
2月前
|
Java 开发者
Java多线程初学者指南:介绍通过继承Thread类与实现Runnable接口两种方式创建线程的方法及其优缺点
【10月更文挑战第20天】Java多线程初学者指南:介绍通过继承Thread类与实现Runnable接口两种方式创建线程的方法及其优缺点,重点解析为何实现Runnable接口更具灵活性、资源共享及易于管理的优势。
34 1
|
2月前
|
安全 Java 开发者
Java多线程中的`wait()`、`notify()`和`notifyAll()`方法,探讨了它们在实现线程间通信和同步中的关键作用
本文深入解析了Java多线程中的`wait()`、`notify()`和`notifyAll()`方法,探讨了它们在实现线程间通信和同步中的关键作用。通过示例代码展示了如何正确使用这些方法,并分享了最佳实践,帮助开发者避免常见陷阱,提高多线程程序的稳定性和效率。
38 1
|
2月前
|
Java
在Java多线程编程中,`wait()` 和 `notify()/notifyAll()` 方法是线程间通信的核心机制。
在Java多线程编程中,`wait()` 和 `notify()/notifyAll()` 方法是线程间通信的核心机制。它们通过基于锁的方式,使线程在条件不满足时进入休眠状态,并在条件成立时被唤醒,从而有效解决数据一致性和同步问题。本文通过对比其他通信机制,展示了 `wait()` 和 `notify()` 的优势,并通过生产者-消费者模型的示例代码,详细说明了其使用方法和重要性。
27 1
|
2月前
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
51 6
|
2月前
|
存储 运维 NoSQL
Redis为什么最开始被设计成单线程而不是多线程
总之,Redis采用单线程设计是基于对系统特性的深刻洞察和权衡的结果。这种设计不仅保持了Redis的高性能,还确保了其代码的简洁性、可维护性以及部署的便捷性,使之成为众多应用场景下的首选数据存储解决方案。
42 1