OC 线程操作 - GCD快速迭代

Posted qingzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC 线程操作 - GCD快速迭代相关的知识,希望对你有一定的参考价值。

- (void)forDemo{
    //全都是在主线程操作的
    for (NSInteger i = 0; i<10; i++) {
        NSLog(@"--%@", [NSThread currentThread]);
    }
    
    /*
     2018-06-27 11:33:44.226664+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.226845+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.227048+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.227711+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.227796+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.227927+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.227986+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.228046+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.228111+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     2018-06-27 11:33:44.228165+0800 5线程操作-GCD-快速迭代[7715:77291] --<NSThread: 0x60800006a9c0>{number = 1, name = main}
     */
}

-(void)GCD_apply{
    
    
    /**
     参数1:size_t iterations 便利次数
     参数1:dispatch_queue_t  _Nonnull queue 队列:只能穿进去并发队列,主队列 会死锁 ,串行队列和for循环一样效果在主队列,
     参数1:<#^(size_t)block#> ^(size_t 索引) {
     
     }
     
      主队列也会参与这个操作, 并发执行
     */
    dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
        NSLog(@"dispatch_get_global_queue ---- %zd--%@", index,[NSThread currentThread]);
    });
    
    
    dispatch_queue_t queue = dispatch_queue_create("并发", DISPATCH_QUEUE_CONCURRENT);
    dispatch_apply(10, queue, ^(size_t index) {
        NSLog(@"DISPATCH_QUEUE_CONCURRENT ---- %zd--%@", index,[NSThread currentThread]);
    });
    
    /*
     2018-06-27 11:41:46.924361+0800 5线程操作-GCD-快速迭代[7877:85649] 0--<NSThread: 0x608000074ac0>{number = 1, name = main}
     2018-06-27 11:41:46.924467+0800 5线程操作-GCD-快速迭代[7877:85688] 2--<NSThread: 0x60400007de40>{number = 4, name = (null)}
     2018-06-27 11:41:46.924468+0800 5线程操作-GCD-快速迭代[7877:85691] 3--<NSThread: 0x60c000465cc0>{number = 5, name = (null)}
     2018-06-27 11:41:46.924469+0800 5线程操作-GCD-快速迭代[7877:85690] 1--<NSThread: 0x608000265a00>{number = 3, name = (null)}
     2018-06-27 11:41:46.924526+0800 5线程操作-GCD-快速迭代[7877:85649] 4--<NSThread: 0x608000074ac0>{number = 1, name = main}
     2018-06-27 11:41:46.924554+0800 5线程操作-GCD-快速迭代[7877:85688] 5--<NSThread: 0x60400007de40>{number = 4, name = (null)}
     2018-06-27 11:41:46.924568+0800 5线程操作-GCD-快速迭代[7877:85691] 6--<NSThread: 0x60c000465cc0>{number = 5, name = (null)}
     2018-06-27 11:41:46.924602+0800 5线程操作-GCD-快速迭代[7877:85649] 8--<NSThread: 0x608000074ac0>2018-06-27 11:41:46.924639+0800 5线程操作-GCD-快速迭代[7877:85688] 9--<NSThread: 0x60400007de40>{number = 4, name = (null)}
     {number = 1, name = main}
     2018-06-27 11:41:46.924604+0800 5线程操作-GCD-快速迭代[7877:85690] 7--<NSThread: 0x608000265a00>{number = 3, name = (null)}
     */
}

 

以上是关于OC 线程操作 - GCD快速迭代的主要内容,如果未能解决你的问题,请参考以下文章

[OC学习笔记]GCD复习

[OC学习笔记]GCD复习

GCD快速迭代(遍历)

[OC学习笔记]Grand Central Dispatch

[OC学习笔记]多线程之GCD

[OC学习笔记]多线程之GCD