zoukankan      html  css  js  c++  java
  • ios多线程之GCD

    
    
    **
     dispatch_after 延时操作应用场景
     
     例如:游戏后台需要做一些随机的事件,需要在某个时间后,调用方法!
     
     1> 调用的方法通常是跟UI有关的,例如提示用户等
     2> 不了解GCD或者多线程的人,可以直接填空即可
     
     */
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self delay1];
    }
    
    #pragma mark - 延时操作
    /** 在其他线程中调用 dispatch_after */
    - (void)delay1
    {
        // 1. 队列
        dispatch_queue_t q = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
        
        // 2. 异步任务
        dispatch_async(q, ^{
            NSLog(@"延时开始前.... %@", [NSThread currentThread]);
            // 输入dispatch_after
            // 从当前时间,延迟2.0秒之后,给主队列添加一个任务(此任务会在主线程上【异步】运行)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", [NSThread currentThread]);
            });
            NSLog(@"延时设置结束....");
        });
    }
    
    
    /** 在主线程调用dispatch_after */
    - (void)delay
    {
        NSLog(@"延时开始前....");
        // 输入dispatch_after
        // 从当前时间,延迟2.0秒之后,给主队列添加一个任务(此任务会在主线程上【异步】运行)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", [NSThread currentThread]);
        });
        NSLog(@"延时设置结束....");
    }
    
    @end
    
    
    
    
    **
     dispatch_after 延时操作应用场景
     
     例如:游戏后台需要做一些随机的事件,需要在某个时间后,调用方法!
     
     1> 调用的方法通常是跟UI有关的,例如提示用户等
     2> 不了解GCD或者多线程的人,可以直接填空即可
     
     */
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self delay1];
    }
    
    #pragma mark - 延时操作
    /** 在其他线程中调用 dispatch_after */
    - (void)delay1
    {
        // 1. 队列
        dispatch_queue_t q = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
        
        // 2. 异步任务
        dispatch_async(q, ^{
            NSLog(@"延时开始前.... %@", [NSThread currentThread]);
            // 输入dispatch_after
            // 从当前时间,延迟2.0秒之后,给主队列添加一个任务(此任务会在主线程上【异步】运行)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", [NSThread currentThread]);
            });
            NSLog(@"延时设置结束....");
        });
    }
    
    
    /** 在主线程调用dispatch_after */
    - (void)delay
    {
        NSLog(@"延时开始前....");
        // 输入dispatch_after
        // 从当前时间,延迟2.0秒之后,给主队列添加一个任务(此任务会在主线程上【异步】运行)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", [NSThread currentThread]);
        });
        NSLog(@"延时设置结束....");
    }
    
    @end
     
     
     
     
  • 相关阅读:
    eclipse项目迁移到android studio(图文最新版)
    栈上分配存储器的方法 alloca 抽样
    【PHP】PHP获得第一章
    阿里2015回顾面试招收学历(获得成功offer)
    Linux 于 shell 变数 $#,$@,$0,$1,$2 含义解释:
    Codeforces 451E Devu and Flowers(容斥原理)
    hdu 4964 Emmet()模拟
    “度”思考
    Windows Auzre 微软的云计算产品的后台操作界面
    Java设计模式菜鸟系列(两)建模与观察者模式的实现
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3958934.html
Copyright © 2011-2022 走看看