zoukankan      html  css  js  c++  java
  • 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)}
    
  • 相关阅读:
    大战设计模式【5】—— 工厂方法模式
    通过spring抽象路由数据源+MyBatis拦截器实现数据库自动读写分离
    大战设计模式【4】—— 简单工厂模式
    大战设计模式【3】—— 装饰模式
    大战设计模式【2】—— 观察者模式
    大战设计模式【1】—— 策略模式
    回顾:maven配置和常用命令整理
    idea properties文件unicode码问题
    Nginx学习笔记
    tomcat添加context方式部署web应用
  • 原文地址:https://www.cnblogs.com/HJiang/p/7467923.html
Copyright © 2011-2022 走看看