iOS多线程 GCD使用

2017-12-13  本文已影响27人  docallsama

各种方法

  1. 获取主线程队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
  1. 获取global子线程队列
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

关于子线程队列priority,执行完High,再执行Default,然后是Low,最后是Backgound。

  1. 创建自定义队列
dispatch_queue_t customQueue = dispatch_queue_create("com.uzai.testQueue", DISPATCH_QUEUE_CONCURRENT);

创建队列,serial串行执行,concurrent并行执行

  1. 异步执行
dispatch_async(customQueue, ^{
    NSLog(@"custom serial queue %@",[NSThread currentThread]);
});
  1. 同步执行
dispatch_sync(customQueue, ^{
    NSLog(@"custom serial queue %@",[NSThread currentThread]);
});

异步执行串行队列,同一子线程串行执行
异步执行并行队列,多个子线程并行执行
同步执行串行队列,主线程串行执行
同步执行并行队列,主线程串行执行

  1. 按次数执行block

按照指定次数将 block 添加到指定的队列当中,dispatch_apply函数会等待全部处理结束。

dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSArray *wordsArray = @[@"GCD",@"NSOperations",@"NSOperationQueues",@"algorithms"];
dispatch_apply(wordsArray.count, asyncQueue, ^(size_t currentIndex) {
    NSLog(@"current size -> %@",wordsArray[currentIndex]);
});
NSLog(@"all applies are done");
  1. 创建单例
+ (instancetype)shareInstance {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
  1. dispatch_group
dispatch_queue_t current1 = dispatch_queue_create("current1", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, current1, ^{
    sleep(3);
    NSLog(@"current 1 finish %@",[NSThread currentThread]);
});

dispatch_group_async(group, current1, ^{
    sleep(3);
    NSLog(@"current 2 finish %@",[NSThread currentThread]);
});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    NSLog(@"all finish");
});

//output
//2017-12-07 15:57:59.207 iOSInterviewProblems[64337:3876851] current 1 finish <NSThread: 0x60800026f6c0>{number = 9, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3945895] current 2 finish <NSThread: 0x600000270800>{number = 12, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3875012] all finish
  1. dispatch_group_enter 在任务开始执行时调用
  2. dispatch_group_leave 在任务完成时调用
dispatch_group_t downloadGroup = dispatch_group_create();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.uzai.download", DISPATCH_QUEUE_CONCURRENT);

for (int i = 0; i < 6; i++) {
    dispatch_group_enter(downloadGroup);
    dispatch_async(downloadQueue, ^{
        NSLog(@"now downloading");
        sleep(i);
        dispatch_group_leave(downloadGroup);
        NSLog(@"downloaded");
    });
}

dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
    NSLog(@"all download done");
});

//output
//    2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] now downloading
//    2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995991] now downloading
//    2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995994] now downloading
//    2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] downloaded
//    2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996047] now downloading
//    2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996044] now downloading
//    2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996048] now downloading
//    2017-12-07 17:06:25.840 iOSInterviewProblems[65166:3995991] downloaded
//    2017-12-07 17:06:26.766 iOSInterviewProblems[65166:3995994] downloaded
//    2017-12-07 17:06:27.840 iOSInterviewProblems[65166:3996047] downloaded
//    2017-12-07 17:06:28.766 iOSInterviewProblems[65166:3996044] downloaded
//    2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3996048] downloaded
//    2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3995950] all download done
  1. dispatch_sources

GCD提供一系列的dispatch sources,用来监控活动的状况(底层系统活动,例如Unix 信号,VFS 节点 等等)。并且在上述活动发生的时候,执行提交到队列的事务。

//创建并启动timer
if (self.timerSource == nil) {
    self.timerCountDown = 0;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timerSource, dispatch_walltime(NULL, 0), 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(self.timerSource, ^{
        self.timerCountDown++;
        NSLog(@"printting now -> %d",self.timerCountDown);
    });
    dispatch_resume(self.timerSource);
}

//暂停timer
if (self.timerSource) {
    dispatch_suspend(self.timerSource);
}

//取消掉timer
if (self.timerSource) {
    dispatch_source_cancel(self.timerSource);
}

示例参见 github Demo

注意事项

上一篇 下一篇

猜你喜欢

热点阅读