IOS学习 GCD 延时执行三种方法 并行/串行/主队列综合练习 队列组 shift+command+o快速查找

来源:互联网 发布:美国钻井平台数据最新 编辑:程序博客网 时间:2024/06/04 04:36

http://www.cnblogs.com/pure/archive/2013/03/31/2977420.html

http://www.cocoachina.com/industry/20140428/8248.html


http://blog.csdn.net/samuelltk/article/details/9452203/


按shift+command+o快速查找



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{   

    [selfdemo];

}


//延时执行

-(void)demo{

    //方法1 timer

//    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];

//    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    

    //方法2

    [selfperformSelector:@selector(task)withObject:nilafterDelay:3];

    

    //方法3

    /*参数1:延时时间  dispatch_time生成时间 纳秒为计时单位 精度高

     *参数2:队列

     *参数3:任务

     *异步执行       */

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

        NSLog(@"task");

    });

    NSLog(@"over");

}


-(void)task{

    NSLog(@"task");

}


//模拟下载任务

/*1、下载 L01.zip

 *2、下载 L02.zip

 *3、通知UI:下载完成

 123之前执行

 */

-(void)demo1{

    //串行,异步顺序下载

    dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);

    

    dispatch_async(serialQueue, ^{

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间

        dispatch_async(serialQueue, ^{

            NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);

        });

    

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间

        dispatch_async(serialQueue, ^{

            NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);

        });

    

        dispatch_async(serialQueue, ^{

            dispatch_async(dispatch_get_main_queue(), ^{

                NSLog(@"%@下载完成",[NSThreadcurrentThread]);

            });

        });

    });

}


-(void)demo2{

    //并行异步   "shift+command+o"快速查找

    dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);

    

    dispatch_async(concurrentQueue, ^{

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间

        //并行同步顺序下载

        dispatch_sync(concurrentQueue, ^{

             NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);

        });

        

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间

        dispatch_sync(concurrentQueue, ^{

            NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);

        });

        

        dispatch_async(dispatch_get_main_queue(), ^{

            NSLog(@"%@下载完成",[NSThreadcurrentThread]);

        });

    });

}


//任务12在子线程上先顺序执行后,任务34在主线程上执行,最后任务567在子线程上并发无序执行

-(void)demo3{

    dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);

    dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);

    

    dispatch_async(concurrentQueue, ^{

        dispatch_sync(serialQueue, ^{

            NSLog(@"%@执行任务1",[NSThreadcurrentThread]);

        });

        

        dispatch_sync(serialQueue, ^{

            NSLog(@"%@执行任务2",[NSThreadcurrentThread]);

        });

        

        dispatch_sync(dispatch_get_main_queue(), ^{

            NSLog(@"%@执行任务3",[NSThreadcurrentThread]);

        });


        dispatch_sync(dispatch_get_main_queue(), ^{

            NSLog(@"%@执行任务4",[NSThreadcurrentThread]);

        });

        

        dispatch_async(concurrentQueue, ^{

            dispatch_async(concurrentQueue, ^{

                NSLog(@"%@执行任务5",[NSThreadcurrentThread]);

            });

            dispatch_async(concurrentQueue, ^{

                NSLog(@"%@执行任务6",[NSThreadcurrentThread]);

            });

            dispatch_async(concurrentQueue, ^{

                NSLog(@"%@执行任务7",[NSThreadcurrentThread]);

            });

        });

    });

}


//队列组

-(void)demo4{

    NSLog(@"begin");

    //创建队列组

    dispatch_group_t group =dispatch_group_create();

    

    //开启异步任务

    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{

        //模拟网络卡

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间

        NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);

    });

    

    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{

        //模拟网络卡

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间

        NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);

    });


    dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{

        NSLog(@"%@下载完成",[NSThreadcurrentThread]);

    });

}


-(void)demo5{

/*    dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)

    {

        dispatch_retain(group);

        dispatch_group_enter(group);

        dispatch_async(queue, ^{

            block();

            dispatch_group_leave(group);

            dispatch_release(group);

        });     */

    

    dispatch_group_t group =dispatch_group_create();

    

    dispatch_group_enter(group);

    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{

        //模拟网络卡

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间

        NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);

        dispatch_group_leave(group);

    });

    

    dispatch_group_enter(group);

    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{

        //模拟网络卡

        [NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间

        NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);

        dispatch_group_leave(group);

    });


    dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{

        NSLog(@"%@下载完成",[NSThreadcurrentThread]);

    });

}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一年级小朋友不爱写字怎么办 幼儿园小朋友不爱写字怎么办 孩子懒散不积极怎么办 孩子不肯上幼儿园怎么办 孩子不肯去幼儿园怎么办 小孩记不住字怎么办 小孩不会写字要怎么办 一年级孩子不爱写字怎么办 一年级小孩不爱写字怎么办 孩子不爱写字怎么办呢 幼儿园孩子不爱写字怎么办 孩子上学没学籍怎么办 孩子上学务工证怎么办 孩子上学被欺负怎么办 孩子害怕上幼儿园怎么办 孩子写字肩膀疼怎么办 5岁不会写字怎么办 上中班不爱写字怎么办 孩子性子太慢怎么办 13小孩特别懒怎么办 小孩不肯上幼儿园怎么办 宝宝不肯上幼儿园怎么办 宝宝不肯去幼儿园怎么办 小孩子不肯去幼儿园怎么办 上幼儿园不说话怎么办 小孩写字不认真怎么办 游戏打开是乱码怎么办 小孩不写字该怎么办 小孩不喜欢穿袜子怎么办 宝宝不喜欢穿袜子怎么办 看到婆婆就烦怎么办 什么也不想吃怎么办 宝宝不喜欢带围兜怎么办 宝宝宝宝不喜欢脐疝带怎么办 中班小孩不写字怎么办 中班小孩不愿意写字怎么办 中班了不会写字怎么办 胃不舒服没胃口怎么办 不喜欢婆婆带孩子怎么办 小孩咳嗽吃饭吐怎么办 小孩吐不吃饭怎么办