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
     
     
     
     
  • 相关阅读:
    JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    在MOSS中使用无刷新的日历日程控件
    VCalendar不错的开源日历项目
    非常适用的Exchange 2007 Web Services
    在C#中实现DateDiff功能
    Div被Select挡住的解决办法
    安装Project Server2007出现错误
    vs2005中调试js(转)
    CrystalReports在MOSS下的新问题:来自磁盘上的图片不能显示
    关于多级审批工作流的问题描述
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3958934.html
Copyright © 2011-2022 走看看