• 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
     
     
     
     
  • 相关阅读:
    c++ 模板<template class T>
    HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
    用1年的经验做了10年还是,用10年的经验做一件事.
    last_inset_id()mysql注意
    小心变成这样一个人!!!
    主动哥
    转:开个小书店。。呵呵
    mysql 更改主键信息
    磁盘预录
    评估项目
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3958934.html
Copyright © 2020-2023  润新知