队列(Dispatch Queue)
串行队列(Serial Dispatch Queue):
在一个线程中一个接一个的执行任务并发队列(Concurrent Dispatch Queue):
可以让多个任务并发的执行(异步函数下)主队列:(Main Queue GCD自带的一种特殊的串行队列)所有放在主队列的任务默认的都会放在主线程中执行
创建方式:
- dispatch_queue_create 来创建队列,需要传入两个参数 ,第一个参数表示队列的唯一标示,第二个参数用来识别是串行队列还是并发队列。DISPATCH_QUEUE_SERIAL 表示串行队列,DISPATCH_QUEUE_CONCURRENT 表示并发队列。
- GCD 提供了的一种特殊的串行队列:主队列(Main Dispatch Queue)。所有放在主队列中的任务,都会放到主线程中执行。
- 并发队列中,GCD 默认提供了全局并发队列(Global Dispatch Queue)。可以使用dispatch_get_global_queue来获取。需要传入两个参数。第一个参数表示队列优先级,一般用DISPATCH_QUEUE_PRIORITY_DEFAULT。第二个参数暂时没用,用0即可,可使用dispatch_get_main_queue()获得主队列。
任务:
- 同步任务(sync):同步添加任务到指定的队列中,会立即执行,再任务执行完会一直等待,直到任务完成后再继续,并且只能在当前线程下执行
- 异步任务(async) :添加到指定队列中不会做任何等待,可以继续执行后面的任务。具备开启新线程的能力
1.主队列+同步
-(void)method1{
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@",[NSThread currentThread]);
});
}
同步执行的任务不会开线程,并在当前线程中立即执行,由于在主队列中任务默认会在主线程中执行,执行的过程中加入了新的同步任务,同步任务加入后会立即执行,所以ViewDidLoad就等待添加进去的同步任务执行,但是主队的任务是顺序执行,前面的任务执行完才会执行后面的任务,所以添加进去的任务就会等待前面的任务执行,就造成互相等待(死锁)
2.主队列+异步执行
-(void)method2{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"2====%@",[NSThread currentThread]);
});
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"3====%@",[NSThread currentThread]);
});
NSLog(@"4===%@",[NSThread currentThread]);
}
2019-07-04 14:09:37.734707+0800 GCDDemo[6378:157260] 1==={number = 1, name = main}
2019-07-04 14:09:37.734987+0800 GCDDemo[6378:157260] 4==={number = 1, name = main}
2019-07-04 14:09:37.751950+0800 GCDDemo[6378:157260] 2===={number = 1, name = main}
2019-07-04 14:09:37.752778+0800 GCDDemo[6378:157260] 3===={number = 1, name = main}
打印结果证明 主队列异步不会开启新的线程,会先执行完主线程上的代码,然后在主线程顺序执行任务
3.异步执行+并发队列
-(void)method3{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"6===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"7===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"8===%@",[NSThread currentThread]);
});
NSLog(@"9===%@",[NSThread currentThread]);
}
2019-07-04 14:31:39.379089+0800 GCDDemo[6853:174042] 1==={number = 1, name = main}
2019-07-04 14:31:39.379335+0800 GCDDemo[6853:174042] 6==={number = 1, name = main}
2019-07-04 14:31:39.379360+0800 GCDDemo[6853:174151] 2==={number = 3, name = (null)}
2019-07-04 14:31:39.379388+0800 GCDDemo[6853:174153] 3==={number = 4, name = (null)}
2019-07-04 14:31:39.379404+0800 GCDDemo[6853:174152] 4==={number = 5, name = (null)}
2019-07-04 14:31:39.379418+0800 GCDDemo[6853:174155] 5==={number = 6, name = (null)}
由此可以看出异步会开启新的线程,并发队列不会按照添加的顺讯去顺序的执行,CPU会在各个队列中跳来跳去,并且没有先后顺序之分,但是并不是创建了多少个并发任务就会开辟多少个线程,这个线程数量由XNU内核来决定,将上面的案例调整为8个任务,可以看到有些线程是重复的,表明了如果一开始就可以开辟8个线程,就不会出现这种情况,所以当一个任务结束之后,下一个任务可以放到完成的线程中,但是和串行队列还是有区别的
4.并发队列+同步执行
-(void)method4{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5===%@",[NSThread currentThread]);
});
NSLog(@"6===%@",[NSThread currentThread]);
}
2019-07-04 14:43:18.453277+0800 GCDDemo[7138:185328] 1==={number = 1, name = main}
2019-07-04 14:43:18.453472+0800 GCDDemo[7138:185328] 2==={number = 1, name = main}
2019-07-04 14:43:18.453586+0800 GCDDemo[7138:185328] 3==={number = 1, name = main}
2019-07-04 14:43:18.453696+0800 GCDDemo[7138:185328] 4==={number = 1, name = main}
2019-07-04 14:43:18.453805+0800 GCDDemo[7138:185328] 5==={number = 1, name = main}
2019-07-04 14:43:18.453917+0800 GCDDemo[7138:185328] 6==={number = 1, name = main}
同步并发不会开新的线程,在当前线程顺序执行,同步加入的任务会立即执行,由此可见并发队列在同步执行下将失去意义
5.串行队列+同步执行
-(void)method5{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5===%@",[NSThread currentThread]);
});
NSLog(@"6===%@",[NSThread currentThread]);
}
2019-07-04 15:26:49.236486+0800 GCDDemo[8062:223401] 1==={number = 1, name = main}
2019-07-04 15:26:49.236721+0800 GCDDemo[8062:223401] 2==={number = 1, name = main}
2019-07-04 15:26:49.236845+0800 GCDDemo[8062:223401] 3==={number = 1, name = main}
2019-07-04 15:26:49.236965+0800 GCDDemo[8062:223401] 4==={number = 1, name = main}
2019-07-04 15:26:49.237074+0800 GCDDemo[8062:223401] 5==={number = 1, name = main}
2019-07-04 15:26:49.237218+0800 GCDDemo[8062:223401] 6==={number = 1, name = main}
同步串行 在当前线程下顺序执行不会开启新的线程
6.串行队列 + 异步执行
-(void)method6{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5===%@",[NSThread currentThread]);
});
NSLog(@"6===%@",[NSThread currentThread]);
}
2019-07-04 15:29:17.657894+0800 GCDDemo[8131:225838] 1==={number = 1, name = main}
2019-07-04 15:29:17.658173+0800 GCDDemo[8131:225838] 6==={number = 1, name = main}
2019-07-04 15:29:17.658187+0800 GCDDemo[8131:225907] 2==={number = 3, name = (null)}
2019-07-04 15:29:17.658299+0800 GCDDemo[8131:225907] 3==={number = 3, name = (null)}
2019-07-04 15:29:17.658414+0800 GCDDemo[8131:225907] 4==={number = 3, name = (null)}
2019-07-04 15:29:17.658628+0800 GCDDemo[8131:225907] 5==={number = 3, name = (null)}
开启了一条新线程,异步执行具备开启新线程的能力,由于是串行队列一个执行完才会执行下一个,所以只会开启一个线程。
7线程间通信
-(void)method7{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
});
NSLog(@"4===%@",[NSThread currentThread]);
}
线程间通信
2019-07-04 15:36:53.252351+0800 GCDDemo[8318:233169] 1==={number = 1, name = main}
2019-07-04 15:36:53.252586+0800 GCDDemo[8318:233169] 4==={number = 1, name = main}
2019-07-04 15:36:53.252592+0800 GCDDemo[8318:233235] 2==={number = 3, name = (null)}
2019-07-04 15:36:53.270320+0800 GCDDemo[8318:233169] 3==={number = 1, name = main}
异步任务在分线程中执行,完成后回到主线程操作,开发中现在很少遇到分线程中请求数据回到主线程中刷新UI,其实AFNetworking默认是在主线程返回数据的,所以才不会有这样的体验,如果设置设置AFHTTPSessionManager的completionQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),就需要自己返回主线程刷新界面了。
8栅栏函数
-(void)method8{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3===%@",[NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5===%@",[NSThread currentThread]);
});
NSLog(@"6===%@",[NSThread currentThread]);
}
2019-07-04 15:41:48.562179+0800 GCDDemo[8434:237723] 1==={number = 1, name = main}
2019-07-04 15:41:48.562515+0800 GCDDemo[8434:237723] 6==={number = 1, name = main}
2019-07-04 15:41:48.562528+0800 GCDDemo[8434:237768] 2==={number = 3, name = (null)}
2019-07-04 15:41:48.562577+0800 GCDDemo[8434:237769] 3==={number = 4, name = (null)}
2019-07-04 15:41:48.562722+0800 GCDDemo[8434:237769] 4==={number = 4, name = (null)}
2019-07-04 15:41:48.562962+0800 GCDDemo[8434:237769] 5==={number = 4, name = (null)}
由此可见函数的执行顺序为:先执行栅栏函数前的任务,再执行栅栏函数中的任务,最后执行栅栏后面的任务,
当开发中遇到一个请求需要依赖其他请求的结果时就可以使用栅栏函数,当然还有别的方法可用
9任务组
-(void)method9{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"2====%@",[NSThread currentThread]);
});
dispatch_notify(group, dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"4====%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"3====%@",[NSThread currentThread]);
});
NSLog(@"5===%@",[NSThread currentThread]);
}
2019-07-04 16:01:42.226938+0800 GCDDemo[8906:256993] 1==={number = 1, name = main}
2019-07-04 16:01:42.227168+0800 GCDDemo[8906:256993] 5==={number = 1, name = main}
2019-07-04 16:01:43.231789+0800 GCDDemo[8906:257110] 3===={number = 4, name = (null)}
2019-07-04 16:01:43.231789+0800 GCDDemo[8906:257106] 2===={number = 3, name = (null)}
2019-07-04 16:01:44.233364+0800 GCDDemo[8906:256993] 4===={number = 1, name = main}
任务组中任务的完成才会触发 dispatch_group_notify 函数,即使在最后追加的任务也会在最后触发完成通知方法之前,比如需要等待多个网络请求成功后做一些操作。可以使用该方式,任务组不会卡中当前线程。
10dispatch_group_wait
-(void)method10{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"2====%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"3====%@",[NSThread currentThread]);
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"5===%@",[NSThread currentThread]);
}
2019-07-04 16:10:43.762637+0800 GCDDemo[9168:267297] 1==={number = 1, name = main}
2019-07-04 16:10:44.764665+0800 GCDDemo[9168:267346] 3===={number = 4, name = (null)}
2019-07-04 16:10:44.764682+0800 GCDDemo[9168:267345] 2===={number = 3, name = (null)}
2019-07-04 16:10:44.764875+0800 GCDDemo[9168:267297] 5==={number = 1, name = main}
dispatch_group_wait函数阻塞当前线程,等待 group 中的任务执行完成后,才会往下继续执行。
11dispatch_group_enter&dispatch_group_leave
-(void)method11{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"2===%@",[NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
for (int i =0; i<2; i++) {
NSLog(@"3===%@",[NSThread currentThread]);
}
dispatch_group_leave(group);
});
dispatch_notify(group, queue, ^{
NSLog(@"4===%@",[NSThread currentThread]);
});
NSLog(@"5===%@",[NSThread currentThread]);
}
2019-07-04 16:15:51.370409+0800 GCDDemo[9297:272244] 1==={number = 1, name = main}
2019-07-04 16:15:51.370685+0800 GCDDemo[9297:272244] 5==={number = 1, name = main}
2019-07-04 16:15:51.370691+0800 GCDDemo[9297:272298] 2==={number = 3, name = (null)}
2019-07-04 16:15:51.370725+0800 GCDDemo[9297:272297] 3==={number = 4, name = (null)}
2019-07-04 16:15:51.370871+0800 GCDDemo[9297:272297] 3==={number = 4, name = (null)}
2019-07-04 16:15:51.371012+0800 GCDDemo[9297:272298] 4==={number = 3, name = (null)}
效果跟dispatch_group_async的效果一样
信号量
-(void)method12{
NSLog(@"1===%@",[NSThread currentThread]);
dispatch_semaphore_t semapore = dispatch_semaphore_create(0);//创建信号量
dispatch_queue_t queue = dispatch_queue_create("gcddemo.tg", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i = 0; i<6; i++) {
NSLog(@"2===%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
}
dispatch_semaphore_signal(semapore);//提升信号量
});
dispatch_semaphore_wait(semapore, DISPATCH_TIME_FOREVER);//信号量-1并时候等待,待信号提升后继续执行
NSLog(@"3===%@",[NSThread currentThread]);
}
2019-07-04 16:29:13.815216+0800 GCDDemo[10724:286821] 1==={number = 1, name = main}
2019-07-04 16:29:13.815553+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:15.818078+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:17.823320+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:19.825152+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:21.825917+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:23.827797+0800 GCDDemo[10724:286874] 2==={number = 3, name = (null)}
2019-07-04 16:29:25.829023+0800 GCDDemo[10724:286821] 3==={number = 1, name = main}
使用信号量会卡住当前线程。