GCD dispatch_apply基本使用

Posted HJiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GCD dispatch_apply基本使用相关的知识,希望对你有一定的参考价值。

dispatch_apply,可以并发的循环迭代,性能上可以提高,前提是循环不在乎迭代的顺序

    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        dispatch_apply(10, queue, ^(size_t i) {
            NSLog(@"%zd %@",i,[NSThread currentThread]);
        });
    });
    
    NSLog(@"fun end...");
    
    结果:1.此方式迭代的新开的线程含有当前线程,也就是会阻塞当前线程,如果当前是在主线程的话可能会影响用户点击事件处理/操作 2.只有迭代任务完成后,迭代的后的任务才能被执行
    2017-09-02 21:13:31.709 GCD测试[16672:349258] fun end...
    2017-09-02 21:13:31.709 GCD测试[16672:349297] 0 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.709 GCD测试[16672:349297] 1 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.709 GCD测试[16672:349297] 2 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.709 GCD测试[16672:349297] 3 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 4 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 5 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 6 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 7 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 8 <NSThread: 0x608000260000>{number = 3, name = (null)}
    2017-09-02 21:13:31.710 GCD测试[16672:349297] 9 <NSThread: 0x608000260000>{number = 3, name = (null)}

 

如果不想让在当前线程参与迭代任务,可以把整个迭代任务包含在一个新的线程中 (优先采用此方式),  如:

    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_apply(10, queue, ^(size_t i) {
            NSLog(@"%zd %@",i,[NSThread currentThread]);
        });
    });
    
    NSLog(@"fun end...");
    
    2017-09-02 21:18:32.140 GCD测试[16774:354627] fun end...
    2017-09-02 21:18:32.141 GCD测试[16774:354715] 1 <NSThread: 0x600000078380>{number = 4, name = (null)}
    2017-09-02 21:18:32.141 GCD测试[16774:354716] 0 <NSThread: 0x60800006b740>{number = 3, name = (null)}
    2017-09-02 21:18:32.141 GCD测试[16774:354737] 3 <NSThread: 0x600000078600>{number = 6, name = (null)}
    2017-09-02 21:18:32.141 GCD测试[16774:354718] 2 <NSThread: 0x608000074c40>{number = 5, name = (null)}
    2017-09-02 21:18:32.141 GCD测试[16774:354715] 4 <NSThread: 0x600000078380>{number = 4, name = (null)}
    2017-09-02 21:18:32.141 GCD测试[16774:354716] 5 <NSThread: 0x60800006b740>{number = 3, name = (null)}
    2017-09-02 21:18:32.142 GCD测试[16774:354737] 6 <NSThread: 0x600000078600>{number = 6, name = (null)}
    2017-09-02 21:18:32.142 GCD测试[16774:354718] 7 <NSThread: 0x608000074c40>{number = 5, name = (null)}
    2017-09-02 21:18:32.142 GCD测试[16774:354715] 8 <NSThread: 0x600000078380>{number = 4, name = (null)}
    2017-09-02 21:18:32.142 GCD测试[16774:354716] 9 <NSThread: 0x60800006b740>{number = 3, name = (null)}

 

但此方式需要注意一个问题,就是新开的线程包含的迭代任务,不要是同一个队列,要不然整个迭代任务会在同一个线程中执行,相当于迭代任务串行执行,如:

dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        dispatch_apply(10, queue, ^(size_t i) {
            NSLog(@"%zd %@",i,[NSThread currentThread]);
        });
    });
    
    NSLog(@"fun end...");
    
    2017-09-02 21:20:03.237 GCD测试[16813:356507] 0 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.237 GCD测试[16813:356407] fun end...
    2017-09-02 21:20:03.237 GCD测试[16813:356507] 1 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.237 GCD测试[16813:356507] 2 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.238 GCD测试[16813:356507] 3 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.238 GCD测试[16813:356507] 4 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.238 GCD测试[16813:356507] 5 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.238 GCD测试[16813:356507] 6 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.238 GCD测试[16813:356507] 7 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.239 GCD测试[16813:356507] 8 <NSThread: 0x600000272900>{number = 3, name = (null)}
    2017-09-02 21:20:03.239 GCD测试[16813:356507] 9 <NSThread: 0x600000272900>{number = 3, name = (null)}

 

以上是关于GCD dispatch_apply基本使用的主要内容,如果未能解决你的问题,请参考以下文章

dispatch_apply的用法

iOS并发编程对比总结,NSThread,NSOperation,GCD - iOS

iOS 多线程:『GCD』详尽总结

多线程的基本认识之三

九使用多线程——NSThread,GCD和NSOperation

iOS 多线程GCD的基本使用