1. GCD 的两个核心 :任务 和 队列
任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码。在 GCD 中是放在 block 中的。执行任务有两种方式:同步执行(sync)和异步执行(async)。两者的主要区别是:是否等待队列的任务执行结束,以及是否具备开启新线程的能力。
-
同步执行(sync):
- 同步添加任务到指定的队列中,在添加的任务执行结束之前,会一直等待,直到队列里面的任务完成之后再继续执行。
- 只能在当前线程中执行任务,不具备开启新线程的能力。
-
异步执行(async):
- 异步添加任务到指定的队列中,它不会做任何等待,可以继续执行任务。
可以在新的线程中执行任务,具备开启新线程的能力。
- 异步添加任务到指定的队列中,它不会做任何等待,可以继续执行任务。
队列(Dispatch Queue):这里的队列指执行任务的等待队列,即用来存放任务的队列。队列是一种特殊的线性表,采用 FIFO(先进先出)的原则,即新任务总是被插入到队列的末尾,而读取任务的时候总是从队列的头部开始读取。每读取一个任务,则从队列中释放一个任务。
- 串行队列(Serial Dispatch Queue):每次只有一个任务被执行。让任务一个接着一个地执行。只开启一个线程,一个任务执行完毕后,再执行下一个任务
- 并行队列(Concurrent Dispatch Queue):可以让多个任务并发(同时)执行。可以开启多个线程,并且同时执行任务
个人理解: 队列(Serial和Concurrent) 决定是否 具备 开启线程的能力,sync和async决定是否 开启 另外的线程
开启线程的能力跟所在的线程没关系
![](https://img.haomeiwen.com/i15505772/e9d20f4b8cab0a60.png)
2. 代码解析
代码1
//创建串行队列
dispatch_queue_t mySerialQueue = dispatch_queue_create("my_test_queue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t myQueue = dispatch_queue_create("my_test_queue_2", DISPATCH_QUEUE_SERIAL);
//异步执行+串行队列:开启一个线程
dispatch_async(mySerialQueue, ^{
//任务1
NSLog(@"7-----%@",[NSThread currentThread]);
NSLog(@"7 === start");
//异步 不用等待 继续执行
dispatch_async(myQueue, ^{
//任务3
NSLog(@"4 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"4-----%@",[NSThread currentThread]);
}
dispatch_async(mySerialQueue, ^{
//任务4
NSLog(@"8 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"8-----%@",[NSThread currentThread]);
}
NSLog(@"8 === end");
});
NSLog(@"4 === end");
});
//异步 不用等待继续执行
dispatch_async(myQueue, ^{
//任务5
NSLog(@"5 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"5-----%@",[NSThread currentThread]);
}
});
NSLog(@"7 === end");
});
//串行队列 需一个一个执行 :等待 NSLog(@"7 === end") 执行后再执行
dispatch_async(mySerialQueue, ^{
//任务2
NSLog(@"9 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"9-----%@",[NSThread currentThread]);
}
NSLog(@"9 === end");
});
执行后输出结果是 :
2019-04-16 14:37:09.149808+0800 test123[95964:8582595] 7-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:09.149973+0800 test123[95964:8582595] 7 === start
2019-04-16 14:37:09.150092+0800 test123[95964:8582595] 7 === end
2019-04-16 14:37:09.150102+0800 test123[95964:8582592] 4 === start
2019-04-16 14:37:09.150225+0800 test123[95964:8582595] 9 === start
2019-04-16 14:37:11.153315+0800 test123[95964:8582595] 9-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:11.153338+0800 test123[95964:8582592] 4-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:13.157628+0800 test123[95964:8582595] 9-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:13.157649+0800 test123[95964:8582592] 4-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:13.158006+0800 test123[95964:8582595] 9 === end
2019-04-16 14:37:13.158021+0800 test123[95964:8582592] 4 === end
2019-04-16 14:37:13.158315+0800 test123[95964:8582592] 5 === start
2019-04-16 14:37:13.158366+0800 test123[95964:8582595] 8 === start
2019-04-16 14:37:15.163083+0800 test123[95964:8582592] 5-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:15.163111+0800 test123[95964:8582595] 8-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:17.163967+0800 test123[95964:8582592] 5-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:17.163967+0800 test123[95964:8582595] 8-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:17.164365+0800 test123[95964:8582595] 8 === end
分析:任务3(4===start) 比 任务2(9===start) 早输出原因:异步执行 任务3 后,下边代码还有耗时操作(任务5、NSLog)所以 任务2 需要等待,会先异步执行 任务3(4 === start )
代码2
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"1 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1-----%@",[NSThread currentThread]);
}
NSLog(@"1 === end");
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"2 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2-----%@",[NSThread currentThread]);
}
NSLog(@"2 === end");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
//创建串行队列
dispatch_queue_t mySerialQueue = dispatch_queue_create("my_test_queue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t myQueue = dispatch_queue_create("my_test_queue_2", DISPATCH_QUEUE_SERIAL);
//异步执行+串行队列:开启一个线程
dispatch_async(mySerialQueue, ^{
NSLog(@"7-----%@",[NSThread currentThread]);
NSLog(@"7 === start");
//异步 不用等待 继续执行
dispatch_async(myQueue, ^{
NSLog(@"4 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"4-----%@",[NSThread currentThread]);
}
dispatch_async(mySerialQueue, ^{
NSLog(@"8 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"8-----%@",[NSThread currentThread]);
}
NSLog(@"8 === end");
});
NSLog(@"4 === end");
});
//异步 不用等待继续执行
dispatch_async(myQueue, ^{
NSLog(@"5 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"5-----%@",[NSThread currentThread]);
}
});
NSLog(@"7 === end");
});
//串行队列 需一个一个执行 :等待 NSLog(@"7 === end") 执行后再执行
dispatch_async(mySerialQueue, ^{
NSLog(@"9 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"9-----%@",[NSThread currentThread]);
}
NSLog(@"9 === end");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"3 === start");
for (int i = 0; i<2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3-----%@",[NSThread currentThread]);
}
});
输出结果:
2019-04-16 14:49:41.670736+0800 test123[96172:8588712] 7-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:41.670807+0800 test123[96172:8588710] 1 === start
2019-04-16 14:49:41.670865+0800 test123[96172:8588711] 2 === start
2019-04-16 14:49:41.670975+0800 test123[96172:8588712] 7 === start
2019-04-16 14:49:41.671074+0800 test123[96172:8588712] 7 === end
2019-04-16 14:49:41.671086+0800 test123[96172:8588709] 4 === start
2019-04-16 14:49:41.671177+0800 test123[96172:8588712] 9 === start
2019-04-16 14:49:43.675862+0800 test123[96172:8588710] 1-----<NSThread: 0x600003597380>{number = 4, name = (null)}
2019-04-16 14:49:43.675871+0800 test123[96172:8588712] 9-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:43.675862+0800 test123[96172:8588711] 2-----<NSThread: 0x60000358af40>{number = 5, name = (null)}
2019-04-16 14:49:43.675864+0800 test123[96172:8588709] 4-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:45.680467+0800 test123[96172:8588711] 2-----<NSThread: 0x60000358af40>{number = 5, name = (null)}
2019-04-16 14:49:45.680547+0800 test123[96172:8588712] 9-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:45.680467+0800 test123[96172:8588710] 1-----<NSThread: 0x600003597380>{number = 4, name = (null)}
2019-04-16 14:49:45.680478+0800 test123[96172:8588709] 4-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:45.680875+0800 test123[96172:8588711] 2 === end
2019-04-16 14:49:45.680874+0800 test123[96172:8588712] 9 === end
2019-04-16 14:49:45.680967+0800 test123[96172:8588709] 4 === end
2019-04-16 14:49:45.680874+0800 test123[96172:8588710] 1 === end
2019-04-16 14:49:45.681135+0800 test123[96172:8588712] 8 === start
2019-04-16 14:49:45.681224+0800 test123[96172:8588709] 5 === start
2019-04-16 14:49:45.681723+0800 test123[96172:8588662] 3 === start
2019-04-16 14:49:47.682394+0800 test123[96172:8588662] 3-----<NSThread: 0x6000035e2940>{number = 1, name = main}
2019-04-16 14:49:47.683973+0800 test123[96172:8588709] 5-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:47.684043+0800 test123[96172:8588712] 8-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:49.683874+0800 test123[96172:8588662] 3-----<NSThread: 0x6000035e2940>{number = 1, name = main}
2019-04-16 14:49:49.684750+0800 test123[96172:8588709] 5-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:49.684799+0800 test123[96172:8588712] 8-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:49.685113+0800 test123[96172:8588712] 8 === end
解析:1和2先后顺序不一定 1、2、4、9相互交替执行,5肯定在4结束后执行,8肯定在9执行完后执行,最后5和8交替执行
网友评论