串行:类似买东西只排了一个队伍,按顺序执行,不分同步异步,先进先出
串行+纯同步:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"666666");
});
}
NSLog(@"777777");
立即阻塞当前线程,执行队列任务,等待全部任务完成后返回原线程。
运行结果如下:
2022-06-16 16:46:51.081025+0800 test[1409:577908] 222222
2022-06-16 16:46:53.082349+0800 test[1409:577908] 44444
2022-06-16 16:46:55.083660+0800 test[1409:577908] 666666
2022-06-16 16:46:55.083903+0800 test[1409:577908] 777777
同步+纯异步:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"666666");
});
}
NSLog(@"777777");
不阻塞当前线程。开辟一个新的线程顺序执行队列任务
运行结果如下:
2022-06-16 16:52:37.516960+0800 test[1412:579327] 777777
2022-06-16 16:52:39.522169+0800 test[1412:579341] 222222
2022-06-16 16:52:41.525529+0800 test[1412:579341] 44444
2022-06-16 16:52:43.530921+0800 test[1412:579341] 666666
同步+异步同步混合:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"666666");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"8888");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"99999");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"aaaaaa");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"bbbbbbb");
});
}
NSLog(@"777777");
立即阻塞当前任务,当队列中最后一个同步执行结束后,立即返回当前任务(即使后面还有异步任务需要执行也会返回),队列中的任务一定是按顺序运行的,不区分同步异步
运行结果如下:
2022-06-16 16:57:05.541766+0800 test[1421:581767] 222222
2022-06-16 16:57:07.543157+0800 test[1421:581748] 44444
2022-06-16 16:57:09.544609+0800 test[1421:581767] 666666
2022-06-16 16:57:11.545975+0800 test[1421:581748] 99999
2022-06-16 16:57:11.546284+0800 test[1421:581748] 777777
2022-06-16 16:57:13.551024+0800 test[1421:581767] bbbbbbb
并行:先进先出。类似买东西排队,一个队伍,但最前面的顾客如果是异步的,则立即换一个新地方买(开辟新线程),后续顾客立即向前移动一个。同步的顾客则停在当前队伍购买,后面的顾客需要等待该顾客完成(即使后面有异步顾客,异步顾客也得等该同步顾客完成才能运行)。
并行+纯同步:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"666666");
});
}
NSLog(@"777777");
立即阻塞当前任务,执行队列任务,队列任务完成后返回当前任务
执行结果:
2022-06-16 17:03:37.613182+0800 test[1427:583818] 222222
2022-06-16 17:03:39.614572+0800 test[1427:583818] 44444
2022-06-16 17:03:41.615903+0800 test[1427:583818] 666666
2022-06-16 17:03:41.616188+0800 test[1427:583818] 777777
并行+纯异步:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"666666");
});
}
NSLog(@"777777");
不影响当前任务,有多少个异步就开辟多少线程。线程完成顺序未知。
运行结果:
2022-06-16 17:09:02.320619+0800 test[1432:585400] 777777
2022-06-16 17:09:04.322568+0800 test[1432:585412] 222222
2022-06-16 17:09:04.325889+0800 test[1432:585414] 44444
2022-06-16 17:09:04.325892+0800 test[1432:585413] 666666
并行+同步异步混合:
//创建串行队列,并设置队列名
static const char* queName = "name";
dispatch_queue_t que = dispatch_queue_create(queName, DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_set_specific(que, queName, &queName, nil);
if (dispatch_get_specific(queName)) {
NSLog(@"1111");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"222222");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"3333");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"44444");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"55555");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"666666");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"88888");
}else{
dispatch_sync(que, ^{
sleep(2);
NSLog(@"999999");
});
}
if (dispatch_get_specific(queName)) {
NSLog(@"aaaaa");
}else{
dispatch_async(que, ^{
sleep(2);
NSLog(@"bbbbbb");
});
}
NSLog(@"777777");
立即阻塞当前任务,队列最前方如果是异步,则开辟新线程,异步立即出队列(即使异步任务没完成)。如果最前方是同步队列,则同步任务完成后出队列,后续任务都需要等待(无论同步异步),同步任务一定是按入栈顺序运行,异步任务完成则是未知的。最后一个同步任务完成后立即返回当前任务(即使还有异步)。
运行结果如下:
2022-06-16 17:26:16.375671+0800 test[1450:591469] 44444
2022-06-16 17:26:16.379838+0800 test[1450:591482] 222222
2022-06-16 17:26:18.377038+0800 test[1450:591469] 999999
2022-06-16 17:26:18.377093+0800 test[1450:591480] 666666
2022-06-16 17:26:18.377342+0800 test[1450:591469] 777777
2022-06-16 17:26:20.379147+0800 test[1450:591480] bbbbbb