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

相关文章
|
4天前
|
算法 Java
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
该博客文章综合介绍了Java并发编程的基础知识,包括线程与进程的区别、并发与并行的概念、线程的生命周期状态、`sleep`与`wait`方法的差异、`Lock`接口及其实现类与`synchronized`关键字的对比,以及生产者和消费者问题的解决方案和使用`Condition`对象替代`synchronized`关键字的方法。
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
|
1天前
|
Java
多线程线程同步
多线程的锁有几种方式
|
8天前
|
调度 Python
|
1天前
|
安全 C# 开发者
【C# 多线程编程陷阱揭秘】:小心!那些让你的程序瞬间崩溃的多线程数据同步异常问题,看完这篇你就能轻松应对!
【8月更文挑战第18天】多线程编程对现代软件开发至关重要,特别是在追求高性能和响应性方面。然而,它也带来了数据同步异常等挑战。本文通过一个简单的计数器示例展示了当多个线程无序地访问共享资源时可能出现的问题,并介绍了如何使用 `lock` 语句来确保线程安全。此外,还提到了其他同步工具如 `Monitor` 和 `Semaphore`,帮助开发者实现更高效的数据同步策略,以达到既保证数据一致性又维持良好性能的目标。
4 0
|
4天前
|
Java UED
基于SpringBoot自定义线程池实现多线程执行方法,以及多线程之间的协调和同步
这篇文章介绍了在SpringBoot项目中如何自定义线程池来实现多线程执行方法,并探讨了多线程之间的协调和同步问题,提供了相关的示例代码。
27 0
|
6天前
|
存储 缓存 安全
聊一聊高效并发之线程安全
该文章主要探讨了高效并发中的线程安全问题,包括线程安全的定义、线程安全的类别划分以及实现线程安全的一些方法。
|
存储 iOS开发
iOS线程字典
iOS线程字典
157 0
|
6天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的世界中,选择正确的平台是关键。本文通过比较安卓和iOS开发的核心差异,揭示平台选择如何影响应用的性能、用户体验和市场覆盖。我们将深入探讨各自的开发环境、编程语言、用户界面设计原则以及发布流程,以帮助开发者和企业做出明智的决策。
27 9
|
3天前
|
移动开发 开发工具 Android开发
探索安卓与iOS开发的差异:技术选择的影响
【8月更文挑战第17天】 在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚。本文通过比较这两个平台的编程语言、开发工具及市场策略,揭示了技术选择对开发者和产品成功的重要性。我们将从开发者的视角出发,深入探讨不同平台的技术特性及其对项目实施的具体影响,旨在为即将步入移动开发领域的新手提供一个清晰的指南,同时给予资深开发者新的思考角度。
|
6天前
|
Java 开发工具 Android开发
探索安卓与iOS开发的差异:从新手到专家的旅程
在数字时代的浪潮中,移动应用开发成为了连接世界的桥梁。本文将带你走进安卓与iOS这两大移动操作系统的开发世界,通过比较它们的编程语言、开发工具和环境、用户界面设计以及市场分布等方面,揭示各自的独特之处。无论你是初涉编程的新手,还是寻求进阶的开发者,这篇文章都将为你提供宝贵的洞见,助你在移动应用开发的征途上一帆风顺。
20 5