GCD中的两个核心概念
任务:执行操作
队列:用来存放任务GCD的使用就2个步骤
1.定制任务:确定想要做的事
2.将任务添加到队列中:
GCD 会自动将队伍中的任务取出,放到对应的线程中执行
任务的取出遵循队列的FIFO原则:先进先出,后进后出。
队列按照任务程序员指定的方式调度任务,执行任务的方法:同步/异步
同步:一个任务没有结束就不会执行下一个任务。
异步:不用等待任务执行完毕,就会执行下一个任务。
-
GCD中有两个用来执行任务的常用函数
用同步方法执行任务,其中 queue 是队列,block 是任务
dispatch_sync(dispatch_queue_t queue, dispatch_block_t blcok);用异步方法执行任务,其中 queue 是队列,block 是任务
dispatch_async(dispatch_queue_t queue, dispatch_block_t blcok);
- 同步和异步的区别
同步:只能在当前线程中执行任务,不具备开启新线程的能力
异步:可以在新线程中执行任务,具备开启新线程的能力。
对于串行队列执行同步任务 和并行队列执行异步任务这里就不再过多介绍了。
废话不多说 直接上代码
- 串行队列+多个异步任务
//串行队列 异步
- (void)serialQueueTest
{
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@" %@", [NSThread currentThread]);
dispatch_async(serialQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片0 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_async(serialQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片1 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_async(serialQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片2 %@ %d",[NSThread currentThread],i);
}];
});
}
- (void)forNumIncrementCondition:(NSUInteger )num actionBlock:(void(^)(int i))actionBlcok
{
for (int a = 0; a < num; a ++)
{
if (actionBlcok) {
actionBlcok(a);
}
}
}
打印结果
<NSThread: 0x600000061e80>{number = 1, name = main}
下载图片0 <NSThread: 0x60400007f480>{number = 4, name = (null)} 0
下载图片0 <NSThread: 0x60400007f480>{number = 4, name = (null)} 1
下载图片0 <NSThread: 0x60400007f480>{number = 4, name = (null)} 2
下载图片0 <NSThread: 0x60400007f480>{number = 4, name = (null)} 3
下载图片0 <NSThread: 0x60400007f480>{number = 4, name = (null)} 4
下载图片1 <NSThread: 0x60400007f480>{number = 4, name = (null)} 0
下载图片1 <NSThread: 0x60400007f480>{number = 4, name = (null)} 1
下载图片1 <NSThread: 0x60400007f480>{number = 4, name = (null)} 2
下载图片1 <NSThread: 0x60400007f480>{number = 4, name = (null)} 3
下载图片1 <NSThread: 0x60400007f480>{number = 4, name = (null)} 4
下载图片2 <NSThread: 0x60400007f480>{number = 4, name = (null)} 0
下载图片2 <NSThread: 0x60400007f480>{number = 4, name = (null)} 1
下载图片2 <NSThread: 0x60400007f480>{number = 4, name = (null)} 2
下载图片2 <NSThread: 0x60400007f480>{number = 4, name = (null)} 3
下载图片2 <NSThread: 0x60400007f480>{number = 4, name = (null)} 4
结论:串行队列 异步任务,会在一个新的子线程当中顺序执行(注意串行队列同步任务会直接在当前线程执行)
- 并发队列+同步任务
//并发队列 同步
- (void)concurrentQueueSyncTest
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"%@",[NSThread currentThread]);
dispatch_sync(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片0 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_sync(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片1 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_sync(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片2 %@ %d",[NSThread currentThread],i);
}];
});
}
- (void)forNumIncrementCondition:(NSUInteger )num actionBlock:(void(^)(int i))actionBlcok
{
for (int a = 0; a < num; a ++)
{
if (actionBlcok) {
actionBlcok(a);
}
}
}
<NSThread: 0x604000078100>{number = 1, name = main}
下载图片0 <NSThread: 0x604000078100>{number = 1, name = main} 0
下载图片0 <NSThread: 0x604000078100>{number = 1, name = main} 1
下载图片0 <NSThread: 0x604000078100>{number = 1, name = main} 2
下载图片0 <NSThread: 0x604000078100>{number = 1, name = main} 3
下载图片0 <NSThread: 0x604000078100>{number = 1, name = main} 4
下载图片1 <NSThread: 0x604000078100>{number = 1, name = main} 0
下载图片1 <NSThread: 0x604000078100>{number = 1, name = main} 1
下载图片1 <NSThread: 0x604000078100>{number = 1, name = main} 2
下载图片1 <NSThread: 0x604000078100>{number = 1, name = main} 3
下载图片1 <NSThread: 0x604000078100>{number = 1, name = main} 4
下载图片2 <NSThread: 0x604000078100>{number = 1, name = main} 0
下载图片2 <NSThread: 0x604000078100>{number = 1, name = main} 1
下载图片2 <NSThread: 0x604000078100>{number = 1, name = main} 2
下载图片2 <NSThread: 0x604000078100>{number = 1, name = main} 3
下载图片2 <NSThread: 0x604000078100>{number = 1, name = main} 4
结论:并发队列 里放置同步任务会在当前线程顺序执行
- 并发队列+2异步+2同步+2异步
//并发队列 2异步 2同步 2 异步
- (void)concurrentQueueAsyncAndSyncTest
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"%@",[NSThread currentThread]);
dispatch_async(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片0 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_async(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片1 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_sync(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片2 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_sync(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片3 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_async(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片4 %@ %d",[NSThread currentThread],i);
}];
});
dispatch_async(concurrentQueue, ^{
[self forNumIncrementCondition:5 actionBlock:^(int i) {
NSLog(@"下载图片5 %@ %d",[NSThread currentThread],i);
}];
});
}
- (void)forNumIncrementCondition:(NSUInteger )num actionBlock:(void(^)(int i))actionBlcok
{
for (int a = 0; a < num; a ++)
{
if (actionBlcok) {
actionBlcok(a);
}
}
}
打印结果
{number = 1, name = main}
下载图片2 {number = 1, name = main} 0
下载图片0 {number = 5, name = (null)} 0
下载图片1 {number = 6, name = (null)} 0
下载图片2 {number = 1, name = main} 1
下载图片0 {number = 5, name = (null)} 1
下载图片1 {number = 6, name = (null)} 1
下载图片2 {number = 1, name = main} 2
下载图片0 {number = 5, name = (null)} 2
下载图片1 {number = 6, name = (null)} 2
下载图片2 {number = 1, name = main} 3
下载图片0 {number = 5, name = (null)} 3
下载图片1 {number = 6, name = (null)} 3
下载图片2 {number = 1, name = main} 4
下载图片0 {number = 5, name = (null)} 4
下载图片1 {number = 6, name = (null)} 4
下载图片3 {number = 1, name = main} 0
下载图片3 {number = 1, name = main} 1
下载图片3 {number = 1, name = main} 2
下载图片3 {number = 1, name = main} 3
下载图片3 {number = 1, name = main} 4
下载图片4 {number = 6, name = (null)} 0
下载图片5 {number = 5, name = (null)} 0
下载图片4 {number = 6, name = (null)} 1
下载图片4 {number = 6, name = (null)} 2
下载图片5 {number = 5, name = (null)} 1
下载图片4 {number = 6, name = (null)} 3
下载图片5 {number = 5, name = (null)} 2
下载图片4 {number = 6, name = (null)} 4
下载图片5 {number = 5, name = (null)} 3
下载图片5 {number = 5, name = (null)} 4
结论:并发队列中同步任务会直接在当前线程执行(相当于耗时操作堵塞了当前线程),执行完成后才会执行下一行代码,而异步任务则会直接开辟另外的线程来执行任务,不会因为任务而堵塞当前线程。
注意:同一个串行队列不能在任务中再添加同步任务,因为同步会在当前线程直接执行,会堵塞当前线程用来执行刚刚添加的同步任务,而根据先入先出原则,后添加的任务应该要放在后边执行,所以会造成线程锁死。
//同一个串行队列不能在任务中再添加同步任务
- (void)globalQueueThreadtest
{
dispatch_queue_t q = dispatch_queue_create("PPD", DISPATCH_QUEUE_SERIAL);
// q = dispatch_get_main_queue();
// q = dispatch_get_global_queue(0, 0);
dispatch_sync(q, ^{
NSLog(@"start %@",[NSThread currentThread]);
dispatch_sync(q, ^{
NSLog(@"continue %@",[NSThread currentThread]);
});
NSLog(@"end %@",[NSThread currentThread]);
});
NSLog(@"end %@ end",[NSThread currentThread]);
}