GCD
任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码。在 GCD 中是放在 block 中的。执行任务有两种方式:同步执行(sync)和异步执行(async)。两者的主要区别是:是否等待队列的任务执行结束,以及是否具备开启新线程的能力。
队列:在 GCD 中有两种队列即串行队列和并发队列。两者都符合 FIFO(先进先出)的原则。两者的主要区别是:执行顺序不同,以及开启线程数不同。
1. 同步+串行(单线程顺序执行,有造成死锁的可能)
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
});
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"2线程===%@", [NSThread currentThread]]);
});
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"3线程===%@", [NSThread currentThread]]);
});
打印结果:
2019-03-18 15:01:36.675180+0800 GCD[83006:13445626] 1线程===<NSThread: 0x6000029b53c0>{number = 1, name = main}
2019-03-18 15:01:36.675425+0800 GCD[83006:13445626] 2线程===<NSThread: 0x6000029b53c0>{number = 1, name = main}
2019-03-18 15:01:36.675567+0800 GCD[83006:13445626] 3线程===<NSThread: 0x6000029b53c0>{number = 1, name = main}
死锁:
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
});
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue(), ^{
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
dispatch_sync(queue(), ^{
NSLog(@"%@", [NSString stringWithFormat:@"2线程===%@", [NSThread currentThread]]);
});
});
2. 同步+并发(单线程顺序执行)
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
});
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"2线程===%@", [NSThread currentThread]]);
});
dispatch_sync(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"3线程===%@", [NSThread currentThread]]);
});
打印结果:
2019-03-18 14:44:00.540615+0800 GCD[80694:13378657] 1线程===<NSThread: 0x600003c08cc0>{number = 1, name = main}
2019-03-18 14:44:00.540869+0800 GCD[80694:13378657] 2线程===<NSThread: 0x600003c08cc0>{number = 1, name = main}
2019-03-18 14:44:00.541038+0800 GCD[80694:13378657] 3线程===<NSThread: 0x600003c08cc0>{number = 1, name = main}
3. 异步+串行(只开启1条新线程,在新线程顺序执行)
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i=0; i<1000; i++) {
NSLog(@"---");
}
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
});
dispatch_async(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"2线程===%@", [NSThread currentThread]]);
});
dispatch_async(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"3线程===%@", [NSThread currentThread]]);
});
打印结果:
2019-03-18 14:49:22.498191+0800 GCD[81423:13397748] ---
2019-03-18 14:49:22.498515+0800 GCD[81423:13397748] ---
2019-03-18 14:49:22.498815+0800 GCD[81423:13397748] ---
2019-03-18 14:49:22.499450+0800 GCD[81423:13397748] 1线程===<NSThread: 0x6000037759c0>{number = 3, name = (null)}
2019-03-18 14:49:22.499753+0800 GCD[81423:13397748] 2线程===<NSThread: 0x6000037759c0>{number = 3, name = (null)}
2019-03-18 14:49:22.500146+0800 GCD[81423:13397748] 3线程===<NSThread: 0x6000037759c0>{number = 3, name = (null)}
4. 异步+并发(多线程异步执行)
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i=0; i<1000; i++) {
NSLog(@"---");
}
NSLog(@"%@", [NSString stringWithFormat:@"1线程===%@", [NSThread currentThread]]);
});
dispatch_async(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"2线程===%@", [NSThread currentThread]]);
});
dispatch_async(queue, ^{
NSLog(@"%@", [NSString stringWithFormat:@"3线程===%@", [NSThread currentThread]]);
});
打印结果:
2019-03-18 14:51:24.275827+0800 GCD[81689:13406579] ---
2019-03-18 14:51:24.276025+0800 GCD[81689:13406579] ---
2019-03-18 14:51:24.276074+0800 GCD[81689:13406580] 3线程===<NSThread: 0x600000947dc0>{number = 4, name = (null)}
2019-03-18 14:51:24.276074+0800 GCD[81689:13406581] 2线程===<NSThread: 0x60000097c1c0>{number = 3, name = (null)}
2019-03-18 14:51:24.717813+0800 GCD[81689:13406579] ---
2019-03-18 14:51:24.718016+0800 GCD[81689:13406579] 1线程===<NSThread: 0x600000974ec0>{number = 5, name = (null)}
关于 dispatch_barrier_async 和 dispatch_group_async
- 栅栏函数
适用场景:任务1和任务2异步执行,任务4和任务5异步执行,并且能保证任务4、任务5绝对在任务3开始执行后执行,执行顺序 = [ 1 || 2 ] > 3 > [ 4 || 5 ]。
注意:栅栏函数只能确定开启异步任务的顺序,并不能阻塞任务的完成,从👇打印的结果可以看出 “任务1” 和 “任务2” 为耗时任务,栅栏函数只能确保 “任务1” 和 “任务2” 的执行为异步的且开启顺序优先于 “任务3” ,而 “任务3” 的开启顺序又优先于 “任务4” “任务5”。
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"开始任务1---%@",[NSThread currentThread]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"结束任务1---%@",[NSThread currentThread]);
});
});
dispatch_async(queue, ^{
NSLog(@"开始任务2---%@",[NSThread currentThread]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"结束任务2---%@",[NSThread currentThread]);
});
});
dispatch_barrier_async(queue, ^{
NSLog(@"开始任务3---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"开始任务4---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"开始任务5---%@",[NSThread currentThread]);
});
打印结果:
2019-12-26 17:54:48.922552+0800 asdf[19648:397317] 开始任务1---<NSThread: 0x600000bcc640>{number = 4, name = (null)}
2019-12-26 17:54:48.922558+0800 asdf[19648:397318] 开始任务2---<NSThread: 0x600000bce540>{number = 5, name = (null)}
2019-12-26 17:54:48.922668+0800 asdf[19648:397318] 开始任务3---<NSThread: 0x600000bce540>{number = 5, name = (null)}
2019-12-26 17:54:48.922746+0800 asdf[19648:397318] 开始任务4---<NSThread: 0x600000bce540>{number = 5, name = (null)}
2019-12-26 17:54:48.922752+0800 asdf[19648:397317] 开始任务5---<NSThread: 0x600000bcc640>{number = 4, name = (null)}
2019-12-26 17:54:51.114557+0800 asdf[19648:397281] 结束任务2---<NSThread: 0x600000b9de40>{number = 1, name = main}
2019-12-26 17:54:53.922602+0800 asdf[19648:397281] 结束任务1---<NSThread: 0x600000b9de40>{number = 1, name = main}
- 组
GCD中“组”的使用可以确保异步任务完成后执行新的任务,可以做到阻塞任务的执行完毕!
注意:dispatch_group_enter()与dispatch_group_leave()要成对使用,否则就会进入无限的等待状态。
dispatch_group_enter:group 中未执行完毕任务数 +1
dispatch_group_leave :group 中未执行完毕任务数 -1。
当 group 中未执行完毕任务数为0的时候,才会使 dispatch_group_wait 解除阻塞,以及执行追加到 dispatch_group_notify 中的任务。
#import "ViewController.h"
@interface ViewController ()
@property(strong, nonatomic)dispatch_group_t group;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self testGCDGroup];
}
-(void)testGCDGroup {
self.group = dispatch_group_create();
dispatch_queue_t concurrent_q = dispatch_queue_create("concurrent_q", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(self.group);
dispatch_group_async(self.group, concurrent_q, ^{
[self task1];
});
dispatch_group_enter(self.group);
dispatch_group_async(self.group, concurrent_q, ^{
[self task2];
});
dispatch_group_notify(self.group, concurrent_q, ^{
[self taskComplete];
});
}
-(void)task1 {
NSLog(@"%@", [NSString stringWithFormat:@"task1执行中,线程===%@", [NSThread currentThread]]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"task1 执行完成");
dispatch_group_leave(self.group);
});
}
-(void)task2 {
NSLog(@"%@", [NSString stringWithFormat:@"task2执行中,线程===%@", [NSThread currentThread]]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"task2 执行完成");
dispatch_group_leave(self.group);
});
}
-(void)taskComplete {
NSLog(@"%@", [NSString stringWithFormat:@"全部执行完毕,线程===%@", [NSThread currentThread]]);
NSLog(@"All task finished.");
}
@end
打印结果:
2019-03-18 11:01:03.152989+0800 GCD[55077:12997726] task1执行中,线程===<NSThread: 0x6000021aab80>{number = 3, name = (null)}
2019-03-18 11:01:03.152989+0800 GCD[55077:12997725] task2执行中,线程===<NSThread: 0x6000021aab40>{number = 4, name = (null)}
2019-03-18 11:01:06.396699+0800 GCD[55077:12997246] task2 执行完成
2019-03-18 11:01:08.649673+0800 GCD[55077:12997246] task1 执行完成
2019-03-18 11:01:08.650026+0800 GCD[55077:12997729] 全部执行完毕,线程===<NSThread: 0x60000219cec0>{number = 5, name = (null)}
2019-03-18 11:01:08.650128+0800 GCD[55077:12997729] All task finished.