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
     
     
     
     
  • 相关阅读:
    HDU 4819 Mosaic --二维线段树(树套树)
    Codeforces Round #274 Div.1 C Riding in a Lift --DP
    ZOJ 3829 Known Notation --贪心+找规律
    JAVA成员变量和静态变量的区别
    spring注解
    js中double类型的数据加减的时候出错
    js在页面间传值的方法记录
    将表中数据转换成java entity实例
    最近都写APP的接口,有苦说不出啊.
    写于2018年第一场雪----记我的第一年工作
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3958934.html
Copyright © 2011-2022 走看看