• 多线程GCD的使用


    dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
    // 处理耗时操作的代码块... 
    
    //通知主线程刷新 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    //回调或者说是通知主线程刷新, 
    }); 
    
    });

    dispatch_async开启一个异步操作,第一个参数是指定一个gcd队列,第二个参数是分配一个处理事物的程序块到该队列。 
    dispatch_get_global_queue(0, 0),指用了全局队列。 
    一般来说系统本身会有3个队列。 
    global_queue,current_queue,以及main_queue. 
    获取一个全局队列是接受两个参数,第一个是我分配的事物处理程序块队列优先级。分高低和默认,0为默认2为高,-2为低 

    #define DISPATCH_QUEUE_PRIORITY_HIGH     2  
    #define DISPATCH_QUEUE_PRIORITY_DEFAULT  0  
    #define DISPATCH_QUEUE_PRIORITY_LOW     (-2)

    GCD异步并行下载

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_group_t group = dispatch_group_create(); 
    dispatch_group_async(group, queue, ^{ 
    [NSThread sleepForTimeInterval:1]; 
    NSLog(@"group1"); 
    }); 
    dispatch_group_async(group, queue, ^{ 
    [NSThread sleepForTimeInterval:2]; 
    NSLog(@"group2"); 
    }); 
    dispatch_group_async(group, queue, ^{ 
    [NSThread sleepForTimeInterval:3]; 
    NSLog(@"group3"); 
    }); 
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
    NSLog(@"updateUi"); 
    }); 
    dispatch_release(group); 

    dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。这个方法很有用,比如你执行三个下载任务,当三个任务都下载完成后你才通知界面说完成的了

    打印:

    [43328:11303] group1
    [43328:12a1b] group2
    [43328:13003] group3
    [43328:f803] updateUi

    dispatch_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行

    dispatch_queue_t queue = dispatch_queue_create("**test.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT); 
    dispatch_async(queue, ^{ 
    [NSThread sleepForTimeInterval:2]; 
    NSLog(@"dispatch_async1"); 
    }); 
    dispatch_async(queue, ^{ 
    [NSThread sleepForTimeInterval:4]; 
    NSLog(@"dispatch_async2"); 
    }); 
    dispatch_barrier_async(queue, ^{ 
    NSLog(@"dispatch_barrier_async"); 
    [NSThread sleepForTimeInterval:4]; 
    }); 
    dispatch_async(queue, ^{ 
    [NSThread sleepForTimeInterval:1]; 
    NSLog(@"dispatch_async3"); 
    });
  • 相关阅读:
    matlab练习程序(最终腐蚀)
    matlab练习程序(二值图像内外边界跟踪)
    matlab练习程序(kmeans聚类)
    matlab练习程序(纹理合成)
    matlab练习程序(图像区域分裂)
    [学习opencv]图像腐蚀、膨胀、开闭操作
    [学习opencv]彩色图像通道分离、合成
    matlab练习程序(条件膨胀)
    matlab练习程序(自适应中值滤波RAMF)
    数据仓库主题设计及元数据设计
  • 原文地址:https://www.cnblogs.com/csdnIOS/p/4807872.html
Copyright © 2020-2023  润新知