美文网首页
GCD的一些方法使用

GCD的一些方法使用

作者: T_aa | 来源:发表于2017-03-08 21:27 被阅读59次

    1.dispatch_barrier_async
    2.dispatch_apply
    3.dispatch_group_t
    4.dispatch_semaphore_t


    dispatch_barrier_async
    dispatch_barrier_async 或者 dispatch_barrier_sync 要添加在自定义的queue中,他会等待这个queue队列中先于他之前的任务执行完成之后再执行。
    阻塞他之后的闭包任务。

    
    //并发的
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.kkkk", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_async(queue, ^{
    //        [NSThread sleepForTimeInterval:5];
            NSLog(@"队列任务kkkk——————1");
            
            NSURL * url = [NSURL URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"];
            NSData * data = [[NSData alloc]initWithContentsOfURL:url];
            UIImage *image = [[UIImage alloc]initWithData:data];
            if (data != nil) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    NSLog(@"队列任务kkkk——————111");
                    
                });
            }
        });
        
        dispatch_async(queue, ^{
            NSLog(@"队列任务kkkk——————2");
        });
        
        dispatch_barrier_async(queue, ^{
            
            NSLog(@"队列任务kkkk------阻塞的队列 barrier 要等其他队列执行完成之后 才能执行吗 ,也必须加载在自己定义的队列中");
            
            [NSThread sleepForTimeInterval:1];
            
        });
        
        
        
        
        dispatch_async(queue, ^{
            
            NSLog(@"队列任务kkkk——————3 会等待barrier执行完成之后再执行");
            [NSThread sleepForTimeInterval:1];
        });
        
        
        
        dispatch_barrier_async(queue, ^{
            
            NSLog(@"队列任务kkkk------测试多个");
            
            [NSThread sleepForTimeInterval:1];
            
        });
        
        
        dispatch_async(queue, ^{
            
            NSLog(@"队列任务kkkk——————4 会等待barrier执行完成之后再执行");
        });
        
        NSLog(@"=========================_+++========++++++");
        
    

    输出

    BangBangXing[4466:70300] +++++++++++++++++++++++===================
    2017-03-08 10:53:31.305 BangBangXing[4466:70300] =========================_+++========++++++
    2017-03-08 10:53:31.305 BangBangXing[4466:70370] 队列任务kkkk——————1
    2017-03-08 10:53:31.305 BangBangXing[4466:70499] 队列任务kkkk——————2
    2017-03-08 10:53:32.363 BangBangXing[4466:70370] 队列任务kkkk------阻塞的队列 barrier 要等其他队列执行完成之后 才能执行吗 ,也必须加载在自己定义的队列中
    2017-03-08 10:53:32.363 BangBangXing[4466:70300] 队列任务kkkk——————111
    2017-03-08 10:53:33.428 BangBangXing[4466:70370] 队列任务kkkk——————3 会等待barrier执行完成之后再执行
    2017-03-08 10:53:34.493 BangBangXing[4466:70370] 队列任务kkkk------测试多个
    2017-03-08 10:53:35.568 BangBangXing[4466:70370] 队列任务kkkk——————4 会等待barrier执行完成之后再执行
    

    dispatch_apply往队列queue中重复添加任务,会阻塞后面的线程,等待他里面添加的任务执行成功之后再往下继续执行。

    NSLog(@"============== begin ==============");
        
        //并发的
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.kkkk", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_async(queue, ^{
    
            [NSThread sleepForTimeInterval:2];
            NSLog(@"队列任务kkkk——————1");
        });
        
        //重复执行 将闭包里面的任务添加到queue中 添加5个
        dispatch_apply(5, queue, ^(size_t i) {
            
            [NSThread sleepForTimeInterval:1];
            
            NSLog(@"队列任务apply添加的kkkk——————%zu",i);
            
        });
        
        dispatch_async(queue, ^{
            
            [NSThread sleepForTimeInterval:2];
            NSLog(@"队列任务kkkk——————2");
        });
        
        
        dispatch_async(queue, ^{
        
            NSLog(@"队列任务kkkk——————3");
        });
        
        NSLog(@"============== end ==============");
        
    

    打印

    2017-03-08 11:15:41.799 BangBangXing[5609:83837] ============== begin ==============
    2017-03-08 11:15:42.801 BangBangXing[5609:83947] 队列任务apply添加的kkkk——————2
    2017-03-08 11:15:42.801 BangBangXing[5609:84041] 队列任务apply添加的kkkk——————1
    2017-03-08 11:15:42.801 BangBangXing[5609:83837] 队列任务apply添加的kkkk——————0
    2017-03-08 11:15:42.801 BangBangXing[5609:83940] 队列任务apply添加的kkkk——————3
    2017-03-08 11:15:43.870 BangBangXing[5609:84018] 队列任务kkkk——————1
    2017-03-08 11:15:43.870 BangBangXing[5609:83947] 队列任务apply添加的kkkk——————4
    2017-03-08 11:15:43.871 BangBangXing[5609:83837] ============== end ==============
    2017-03-08 11:15:43.871 BangBangXing[5609:84018] 队列任务kkkk——————3
    2017-03-08 11:15:45.926 BangBangXing[5609:83947] 队列任务kkkk——————2
    
    

    dispatch_group_t用来监视多个异步任务执行情况,一种是利用dispatch_wait来阻塞当前的进程,等待执行完成或者超时之后继续下一步执行。一种是利用dispatch_group_notify来监听所有任务执行完成之后的回调,不会阻塞,或者和enter和leave配合。

    • dispatch_group_wait
    NSLog(@"============== begin ==============");
        
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.group", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"执行任务1");
        });
        
        dispatch_group_async(group, queue, ^{
            
            [NSThread sleepForTimeInterval:5];
            NSLog(@"执行任务2");
        });
        
        dispatch_group_async(group, queue, ^{
            
            [NSThread sleepForTimeInterval:5];
            NSLog(@"执行任务3");
        });
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"执行任务4");
        });
        
        //设置超时时间
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        
        
        NSLog(@"============== end ==============");
        
    

    打印

    2017-03-08 11:36:57.511 BangBangXing[6987:96675] ============== begin ==============
    2017-03-08 11:36:57.512 BangBangXing[6987:96761] 执行任务1
    2017-03-08 11:36:57.512 BangBangXing[6987:96839] 执行任务4
    2017-03-08 11:37:02.583 BangBangXing[6987:96767] 执行任务3
    2017-03-08 11:37:02.583 BangBangXing[6987:96768] 执行任务2
    2017-03-08 11:37:02.584 BangBangXing[6987:96675] ============== end ==============
    
    • dispatch_group_notify
    NSLog(@"============== begin ==============");
        
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.group", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"执行任务1");
        });
        
        dispatch_group_async(group, queue, ^{
            
            [NSThread sleepForTimeInterval:5];
            NSLog(@"执行任务2");
        });
        
        dispatch_group_async(group, queue, ^{
            
            [NSThread sleepForTimeInterval:5];
            NSLog(@"执行任务3");
        });
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"执行任务4");
        });
        
        dispatch_group_notify(group, queue, ^{
            NSLog(@"所有任务执行完成");
        });
        
        
        NSLog(@"============== end ==============");
    

    打印

    2017-03-08 11:48:24.310 BangBangXing[7362:101941] ============== begin ==============
    2017-03-08 11:48:24.312 BangBangXing[7362:101941] ============== end ==============
    2017-03-08 11:48:24.312 BangBangXing[7362:102007] 执行任务1
    2017-03-08 11:48:24.312 BangBangXing[7362:102000] 执行任务4
    2017-03-08 11:48:29.359 BangBangXing[7362:102095] 执行任务2
    2017-03-08 11:48:29.359 BangBangXing[7362:102009] 执行任务3
    2017-03-08 11:48:29.359 BangBangXing[7362:102009] 所有任务执行完成
    
    • dispatch_group_enter 和 dispatch_group_leave

    enter和leave是成对出现的,开始一个任务之前要先enter 任务完成之后再leave,他们相当于一个dispatch_group_async执行

    NSLog(@"============== begin ==============");
        
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.group", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_enter(group);
        [YHHttps httpsGetWithURL:@"http://www.weather.com.cn/data/sk/101010100.html" andParameterDic:nil andResponseBlock:^(RequestResultObj *resultObj) {
            
            NSLog(@"请求任务1完成");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_enter(group);
        [YHHttps httpsGetWithURL:@"http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=%E9%93%B6%E9%AD%82&bk_length=600" andParameterDic:nil andResponseBlock:^(RequestResultObj *resultObj) {
            
            NSLog(@"请求任务2完成");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_notify(group, queue, ^{
            NSLog(@"所有任务执行完成");
        });
        
        
        NSLog(@"============== end ==============");
    

    输出

    2017-03-08 12:45:56.875 BangBangXing[7999:120101] ============== begin ==============
    2017-03-08 12:45:56.882 BangBangXing[7999:120101] ============== end ==============
    2017-03-08 12:45:57.080 BangBangXing[7999:120101] 请求任务2完成
    2017-03-08 12:45:57.082 BangBangXing[7999:120101] 请求任务1完成
    2017-03-08 12:45:57.082 BangBangXing[7999:120338] 所有任务执行完成
    
    

    dispatch_semaphore_t信号量。
    singal 信号量+1,释放资源了+1
    wait 信号量-1,要占用资源了-1
    信号量总数大于等于0的时候程序继续执行,
    信号量总数小于0的时候阻塞等待

    NSLog(@"============== begin ==============");
        
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        
        dispatch_queue_t queue = dispatch_queue_create("com.lanhai.semaphore", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_async(queue, ^{
            
            [NSThread sleepForTimeInterval:3];
            
            NSLog(@"semaphore + 1");
            
            dispatch_semaphore_signal(semaphore);
            
        });
        
        NSLog(@"semaphore - 1");
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        NSLog(@"============== end ==============");
    

    输出

    2017-03-08 14:07:46.797 BangBangXing[8815:146733] ============== begin ==============
    2017-03-08 14:07:46.797 BangBangXing[8815:146733] semaphore - 1
    2017-03-08 14:07:49.798 BangBangXing[8815:146830] semaphore + 1
    2017-03-08 14:07:49.798 BangBangXing[8815:146733] ============== end ==============
    

    相关文章

      网友评论

          本文标题:GCD的一些方法使用

          本文链接:https://www.haomeiwen.com/subject/utsigttx.html