99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

多線程GCD有哪些常用的函數(shù)

發(fā)布時(shí)間:2016-06-18 22:46  回復(fù):0  查看:3114   最后回復(fù):2016-06-18 22:46  
可能很多iOS小白對iOS多線程函數(shù)比較迷惑,別著急,這里給大家列舉了幾個最常用的 iOS多線程 函數(shù),希望對大家有所幫助。

線程間的通信
從子線程回到主線程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  //執(zhí)行耗時(shí)的異步操作
  dispatch_async(dispatch_get_main_queue(), ^{        
      //回到主線程刷新UI
  });
});

延時(shí)執(zhí)行

iOS常見的延時(shí)執(zhí)行有兩種方式
p 調(diào)用NSObject的方法
[self performSelector:@selector(run:) withObject:nil afterDelay:2.0];

p 使用GCD函數(shù)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      //2秒后異執(zhí)行這里的代碼
  });

一次性代碼

使用dispatch_once函數(shù)能保證某段代碼在執(zhí)行過程中只被執(zhí)行一次

static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{

      //只執(zhí)行一次的代碼(這里默認(rèn)是線程安全的)
  });


dispatch_barrier_async (柵欄函數(shù))

dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);


dispatch_barrier_async的作用是在并行隊(duì)列中,插入其中,就會有等到其前面的線程執(zhí)行完畢,然后執(zhí)行自己的線程,再執(zhí)行其后面的線程。起到一個柵欄的作用


示例代碼 

- (void)barrier
{
  dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);

  dispatch_async(queue, ^{
      NSLog(@"----1-----%@", [NSThread currentThread]);
  });
  dispatch_async(queue, ^{
      NSLog(@"----2-----%@", [NSThread currentThread]);
  });

  dispatch_barrier_async(queue, ^{
      NSLog(@"----barrier-----%@", [NSThread currentThread]);
  });

  dispatch_async(queue, ^{
      NSLog(@"----3-----%@", [NSThread currentThread]);
  });
  dispatch_async(queue, ^{
      NSLog(@"----4-----%@", [NSThread currentThread]);
  });
}

dispatch_apply()

方法
void
dispatch_apply(size_t iterations, dispatch_queue_t queue,
void (^block)(size_t));

參數(shù)
iterations     執(zhí)行的次數(shù)
  queue        提交到的隊(duì)列
  block        執(zhí)行的任務(wù)
  size_t       傳當(dāng)前索引


功能
p 把一項(xiàng)任務(wù)提交到隊(duì)列中多次執(zhí)行,具體是并行執(zhí)行還是串行執(zhí)行由隊(duì)列本身決定.注意,dispatch_apply不會立刻返回,在執(zhí)行完畢后才會返回,是同步的調(diào)用。
p 它可以起到快速迭代的作用,類似于for循環(huán)的遍歷,但for循環(huán)的遍歷是一個一個執(zhí)行的,而dispatch_apply是幾乎同時(shí)執(zhí)行的


示例代碼

/**
* 快速迭代
*/
- (void)apply
{
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  NSString *from = @"/Users/shenzhouguangda/Desktop/icon";
  NSString *to = @"/Users/shenzhouguangda/Desktop/icon1";

  NSFileManager *mgr = [NSFileManager defaultManager];
  NSArray *subpaths = [mgr subpathsAtPath:from];

  dispatch_apply(subpaths.count, queue, ^(size_t index) {
      NSString *subpath = subpaths[index];
      NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
      NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
      // 剪切
      [mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil];

      NSLog(@"%@---%@", [NSThread currentThread], subpath);
  });
}

隊(duì)列組


有這么一種需求
p 首先:分別異步執(zhí)行兩個耗時(shí)操作
p 其次:等兩個異步操作都執(zhí)行完畢后,再回到主線程執(zhí)行操作


如果想要快速高效的實(shí)現(xiàn)上述需求,可以考慮用隊(duì)列組

dispatch_group_t group = dispatch_group_create();

  dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      //執(zhí)行一個耗時(shí)操作
  });

  dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      //執(zhí)行一個耗時(shí)操作

  });

  dispatch_group_notify(group, dispatch_get_main_queue(), ^{

      //等到前面的異步操作都執(zhí)行完畢后,回到主線程。
  });

原文來自:簡書/一抹月光3053
您還未登錄,請先登錄

熱門帖子

最新帖子

?