zoukankan      html  css  js  c++  java
  • GCD深入了解

    一.GCD应用 单例模式

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            NSLog(@"执行一次%@",[NSThread currentThread]);

       });

    二延迟操作

        //1、

        NSLog(@"开始执行");

        //    [self performSelector:@selector(handleAction) withObject:nil afterDelay:0];

        [self performSelector:@selector(handleAction) withObject:nil afterDelay:2];

        //2、

       //DISPATCH_TIME_NOW 从现在开始

        //delayInSeconds 延迟几秒

        dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));

        dispatch_after(time, dispatch_get_main_queue(), ^{

      NSLog(@"%@",[NSThread currentThread]);

       });

    三队列调度

    1.

    //创建调度组

        dispatch_group_t group = dispatch_group_create();

        //获取全局队列

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        //调度组的异步请求

        dispatch_group_async(group, queue, ^{

            NSLog(@"1--%@",[NSThread currentThread]);

            [NSThread sleepForTimeInterval:2];

            NSLog(@"下载第一张图");

        });

        dispatch_group_async(group, queue, ^{

            NSLog(@"2--%@",[NSThread currentThread]);

           [NSThread sleepForTimeInterval:3];

           NSLog(@"下载第二张图");

        });

        dispatch_group_async(group, queue, ^{

            NSLog(@"3--%@",[NSThread currentThread]);

           [NSThread sleepForTimeInterval:1];

          NSLog(@"下载第三张图");

        });

        dispatch_group_async(group, queue, ^{

            NSLog(@"4--%@",[NSThread currentThread]);

            [NSThread sleepForTimeInterval:5];

            NSLog(@"下载第四张图");

        });

        //notify通知,当所有异步请求完成时调用dispatch_group_notify

        dispatch_group_notify(group, queue, ^{

            NSLog(@"5--%@",[NSThread currentThread]);

            NSLog(@"更新UI");

        });

    2

    //创建调度组

        dispatch_group_t group = dispatch_group_create();

        //获取全局队列

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

       //进入队列

        dispatch_group_enter(group);

        dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:2];

            NSLog(@"下载第一张图");

           //离开队列

            dispatch_group_leave(group);

        });

        dispatch_group_enter(group);

        dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:1];

            NSLog(@"下载第er张图");

            dispatch_group_leave(group);

        });

        dispatch_group_enter(group);

        dispatch_async(queue, ^{

           [NSThread sleepForTimeInterval:3];

            NSLog(@"下载第san张图");

            dispatch_group_leave(group);

        });

       //等待调度队列wait相当于一个阻塞状态

        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

        NSLog(@"更新UI");

    四中断操作

    //dispatch_barrier_async一定是自定义队列

        //这里指定的并发队列应该是自己通过dispatch_queue_create函数创建的。如果你传的是一个串行队列或者全局并发队列,这个函数等同于dispatch_async函数。

        dispatch_queue_t queue = dispatch_queue_create("com.bjsxt", DISPATCH_QUEUE_CONCURRENT); //dispatch_get_global_queue(0, 0);

        dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:1];

            NSLog(@"1");

        });    dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:3];

            NSLog(@"2");

        });

        dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:2];

           NSLog(@"3");

        });

       //中断操作dispatch_barrier_async

        dispatch_barrier_async(queue, ^{

            NSLog(@"--------");

            [NSThread sleepForTimeInterval:1];

       });

       dispatch_async(queue, ^{

            [NSThread sleepForTimeInterval:1];

            NSLog(@"4");

        });

        dispatch_async(queue, ^{

            NSLog(@"5");

        });

    五 遍历操作

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        //iterations 遍历的次数

        dispatch_apply(5, queue, ^(size_t i) {

            NSLog(@"%@",@(i));

        });

  • 相关阅读:
    Aix_bugzilla
    aix Mysql安装 Oracle官方教程
    Aix6.1安装openssh
    日媒:阿里巴巴上市融资或超Facebook
    设计模式(一)---单例模式
    Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
    TCP/IP协议族——IP工作原理及实例具体解释(上)
    leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
    Material Design之RecyclerView的使用(一)
    jQuery和CSS3超酷表单美化插件
  • 原文地址:https://www.cnblogs.com/ycg0808/p/5281571.html
Copyright © 2011-2022 走看看