iOS多线程分析2

189 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第6天,点击查看活动详情

1.dispatch_after延时添加到队列

dispatch_time_t delayTime3 = dispatch_time(DISPATCH_TIME_NOW, 3*NSEC_PER_SEC);

dispatch_time_t delayTime2 = dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC);

dispatch_queue_t mainQueue = dispatch_get_main_queue();

NSLog(@"current task");

dispatch_after(delayTime3, mainQueue, ^{

      NSLog(@"3秒之后添加到队列");

});

dispatch_after(delayTime2, mainQueue, ^{

       NSLog(@"2秒之后添加到队列");

});

NSLog(@"next task");

2022-4-715:50:19.369 Whisper[2725:172593] current task

2022-4-7 15:50:19.370 Whisper[2725:172593] next task

2022-4-7 15:50:21.369 Whisper[2725:172593] 2秒之后添加到队列

2022-4-7 15:50:22.654 Whisper[2725:172593] 3秒之后添加到队列

dispatch_after只是延时提交block,并不是延时后立即执行,并不能做到精确控制,需要精确控制的朋友慎用哦

dispatch_apply在给定的队列上多次执行某一任务,在主线程直接调用会阻塞主线程去执行block中的任务。

dispatch_apply函数的功能:把一项任务提交到队列中多次执行,队列可以是串行也可以是并行,dispatch_apply不会立刻返回,在执行完block中的任务后才会返回,是同步执行的函数。

dispatch_apply正确使用方法:为了不阻塞主线程,一般把dispatch_apply放在异步队列中调用,然后执行完成后通知主线程

dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);

NSLog(@"current task");

dispatch_async(globalQueue, ^{

   dispatch_queue_t applyQueue= dispatch_get_global_queue(0, 0);

   //第一个参数,3--block执行的次数

   //第二个参数,applyQueue--block任务提交到的队列

   //第三个参数,block--需要重复执行的任务

   dispatch_apply(3, applyQueue, ^(size_t index) {

          NSLog(@"current index %@",@(index));

          sleep(1);

   });

   NSLog(@"dispatch_apply 执行完成");

   dispatch_queue_t mainQueue= dispatch_get_main_queue();

   dispatch_async(mainQueue, ^{

          NSLog(@"回到主线程更新UI");

  });

});

NSLog(@"next task");

2022-4-7 16:24:45.015 Whisper[4034:202269] current task

2022-4-7 16:24:45.016 Whisper[4034:202269] next task

2022-4-7 16:24:45.016 Whisper[4034:202347] current index 0

2022-4-7 16:24:45.016 Whisper[4034:202344] current index 1

2022-4-7 16:24:45.016 Whisper[4034:202345] current index 2

2022-4-7 16:24:46.021 Whisper[4034:202347] dispatch_apply 执行完成

2022-4-7 16:24:46.021 Whisper[4034:202269] 回到主线程更新UI

嵌套使用dispatch_apply会导致死锁

3.dispatch_once保证在app运行期间,block中的代码只执行一次

//GCD实现单例功能

+ (ShareManager *)shareManager

{

  static dispatch_once_t onceToken;

  dispatch_once(&onceToken, ^{

      sharedManager= [[self alloc] init];

  });

  return sharedManager;

 }

//在ARC下,非GCD,实现单例功能

+ (ShareManager *)sharedManager

{

  @synchronized(self) {

      if (!sharedManager) {

          sharedManager= [[self alloc] init];

      }

  }

  return sharedManager;

 }

4.dispatch_barrier_async 栅栏的作用

功能:是在并行队列中,等待在dispatch_barrier_async之前加入的队列全部执行完成之后(这些任务是并发执行的)再执行dispatch_barrier_async中的任务,dispatch_barrier_async中的任务执行完成之后,再去执行在dispatch_barrier_async之后加入到队列中的任务(这些任务是并发执行的)

dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(conCurrentQueue, ^{

      NSLog(@"dispatch 1");

});

dispatch_async(conCurrentQueue, ^{

       NSLog(@"dispatch 2");

});

dispatch_barrier_async(conCurrentQueue, ^{

       NSLog(@"dispatch barrier");

});

dispatch_async(conCurrentQueue, ^{

       NSLog(@"dispatch 3");

});

dispatch_async(conCurrentQueue, ^{

       NSLog(@"dispatch 4");

});

2022-4-7 18:12:34.125 Whisper[22633:297257] dispatch 1

2022-4-7 18:12:34.125 Whisper[22633:297258] dispatch 2

2022-4-7 18:12:34.126 Whisper[22633:297258] dispatch barrier

2022-4-7 18:12:34.127 Whisper[22633:297258] dispatch 3

2022-4-7 18:12:34.127 Whisper[22633:297257] dispatch 4