GCD队列组

2019-05-29  本文已影响0人  鐧箪
dispatch_group

在多线程的操作中 有时候有多个任务异步去执行 执行完后想统一处理某些事情 就可以考虑用队列组

dispatch_group_t group = dispatch_group_create();
    dispatch_queue_global_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_async(group, quene, ^{
        NSLog(@"线程1 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"1号任务完成啦");
    });
    
    dispatch_group_async(group, quene, ^{
        NSLog(@"线程2 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"2号任务完成啦");
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"任务完成啦");
    });
线程2 --------<NSThread: 0x600000704280>{number = 4, name = (null)}
线程1 --------<NSThread: 0x600000704240>{number = 3, name = (null)}
2号任务完成啦
1号任务完成啦
任务完成啦
dispatch_group_wait 等待
dispatch_group_t group = dispatch_group_create();
    dispatch_queue_global_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_async(group, quene, ^{
        NSLog(@"线程1 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"1号任务完成啦");
    });
    
    dispatch_group_async(group, quene, ^{
        NSLog(@"线程2 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"2号任务完成啦");
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"任务完成啦");
    });
    
    NSLog(@"你在做什么?");
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"你好了没有啊!!!!!");
2019-05-29 14:32:13.713234+0800 GCD[31616:422407] 你在做什么?
线程2 --------<NSThread: 0x60000023a4c0>{number = 4, name = (null)}
线程1 --------<NSThread: 0x60000023d8c0>{number = 3, name = (null)}
 2号任务完成啦
1号任务完成啦
你好了没有啊!!!!!
任务完成啦

可以看出 dispatch_group_wait 阻塞了主线程 等待 group 组内任务完成 才继续向下执行

dispatch_group_enter dispatch_group_leave

自己手动把任务加入 队列组 通过 dispatch_group_enter 添加 任务+1 通过 dispatch_group_leave 移除 任务-1 当任务为0 的时候 执行 dispatch_group_notify dispatch_group_wait

dispatch_group_t group = dispatch_group_create();
    dispatch_queue_global_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_enter(group);
    dispatch_async(quene, ^{
        NSLog(@"线程1 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"1号任务完成啦");
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    dispatch_async(quene, ^{
        NSLog(@"线程2 --------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"2号任务完成啦");
        dispatch_group_leave(group);
    });
    
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"任务完成啦");
    });
    
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"你好了没有啊!!!!!");
线程1 --------<NSThread: 0x60000328e7c0>{number = 3, name = (null)}
线程2 --------<NSThread: 0x600003289480>{number = 4, name = (null)}
1号任务完成啦
2号任务完成啦
你好了没有啊!!!!!
任务完成啦
dispatch_apply GCD快速迭代

遍历任务 并发处理 GCD控制线程数量 不会出现问题

dispatch_queue_t quene = dispatch_queue_create("dy", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(1000, quene, ^(size_t i) {
        NSLog(@"我在哪里啊-----%@",[NSThread currentThread]);
    });
上一篇下一篇

猜你喜欢

热点阅读