zoukankan      html  css  js  c++  java
  • OC 线程操作

    1.队列组两种使用方法
    2.队列组等待 wait

    /** 新方法
     队列组一般用在在异步操作,在主线程写队列组毫无任何作用
     */
    - (void)GCD_Group_new_group___notify{
        dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
        dispatch_group_t group = dispatch_group_create();
        /*
         1.封装任务
         2.把任务加到队列
         3.会监听任务的执行情况
         */
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"1111---%@---", [NSThread currentThread]);
        });
        
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"22222---%@---", [NSThread currentThread]);
        });
        
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"333---%@---", [NSThread currentThread]);
        });
        
        // 一定要加上这行,不然不起任何作用
        dispatch_group_notify(group, globalqueue, ^{
            NSLog(@"完成----4444---%@---", [NSThread currentThread]);
        });
        
        /*
         打印结果:
         2018-06-28 10:18:44.191407+0800 5线程操作-GCD-快速迭代[7808:56262] 333---<NSThread: 0x6000002682c0>{number = 4, name = (null)}---
         2018-06-28 10:18:44.191409+0800 5线程操作-GCD-快速迭代[7808:56266] 1111---<NSThread: 0x608000075300>{number = 3, name = (null)}---
         2018-06-28 10:18:44.191434+0800 5线程操作-GCD-快速迭代[7808:56264] 22222---<NSThread: 0x600000266580>{number = 5, name = (null)}---
         2018-06-28 10:18:44.191758+0800 5线程操作-GCD-快速迭代[7808:56264] 完成----4444---<NSThread: 0x600000266580>{number = 5, name = (null)}---
         */


        //DISPATCH_TIME_FOREVER :等待队列所有任务执行完后 执行下面代码后面的内容
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        NSLog(@"执行完了");
        /*
         打印结果:
         2018-06-28 10:58:34.631990+0800 5线程操作-GCD-快速迭代[8500:86081] 1111---<NSThread: 0x60400007e180>{number = 3, name = (null)}---
         2018-06-28 10:58:34.632041+0800 5线程操作-GCD-快速迭代[8500:86084] 22222---<NSThread: 0x60400007e4c0>{number = 4, name = (null)}---
         2018-06-28 10:58:34.632065+0800 5线程操作-GCD-快速迭代[8500:86080] 333---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
         2018-06-28 10:58:34.633261+0800 5线程操作-GCD-快速迭代[8500:86052] 执行完了
         2018-06-28 10:58:34.633284+0800 5线程操作-GCD-快速迭代[8500:86080] 完成----4444---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
         */ 
    }

     

    //老式写法
    - (void)GCD_GroupDemo_old___enter_leave{
        dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
        dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);
        
        
        // 旧 写法
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"离开---%@---", [NSThread currentThread]);
            dispatch_group_leave(group);
        });
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"222---%@---", [NSThread currentThread]);
        });
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"333---%@---", [NSThread currentThread]);
        });
        
        dispatch_group_async(group, globalqueue, ^{
            NSLog(@"111---%@---", [NSThread currentThread]);
        });
        /*
         2018-06-28 10:25:06.997243+0800 5线程操作-GCD-快速迭代[7942:62493] 离开---<NSThread: 0x60c00007f8c0>{number = 3, name = (null)}---
         2018-06-28 10:25:06.997286+0800 5线程操作-GCD-快速迭代[7942:62491] 333---<NSThread: 0x60800007d240>{number = 5, name = (null)}---
         2018-06-28 10:25:06.997306+0800 5线程操作-GCD-快速迭代[7942:62489] 111---<NSThread: 0x608000260c80>{number = 6, name = (null)}---
         2018-06-28 10:25:06.997287+0800 5线程操作-GCD-快速迭代[7942:62492] 222---<NSThread: 0x60c00007de40>{number = 4, name = (null)}---
         */
    }
    
  • 相关阅读:
    嵌入式驱动开发之sensor---sensor 图形传感器调试
    时下世界上最先进的液晶面板技术---ips
    多媒体开发之rtp 打包发流---udp 丢包问题
    多媒体开发之rtp 打包发流---同网段其他机子sdp 播放不了
    AutoCAD LoadLibrary Failed with error 126 Message
    OpenCV获取与设置像素点的值的几个方法
    四元数与旋转
    圆点博士小四轴主程序状态机结构
    四元数(Quaternion)和旋转 +欧拉角
    PID控制算法
  • 原文地址:https://www.cnblogs.com/qingzZ/p/9237427.html
Copyright © 2011-2022 走看看