zoukankan      html  css  js  c++  java
  • 多线程的延时执行和一次性代码

    延时执行的方法:

    - (void)delay3
    {
        // 3秒后回到主线程执行block中的代码
    //    dispatch_queue_t queue = dispatch_get_main_queue();
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
    //        NSLog(@"------task------%@", [NSThread currentThread]);
    //    });
        
        // 3秒后自动开启新线程 执行block中的代码
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
            NSLog(@"------task------%@", [NSThread currentThread]);
        });
    }
    
    - (void)delay2
    {
        // 一旦定制好延迟任务后,不会卡住当前线程
        [self performSelector:@selector(download:) withObject:@"http://555.jpg" afterDelay:3];
    }
    
    - (void)delay1
    {
        // 延迟执行不要用sleep,坏处:卡住当前线程
        [NSThread sleepForTimeInterval:3];
        NSLog(@"-----下载图片-----");
    }

    一次性代码:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"----touchesBegan");
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSLog(@"----once");
        });
    }
  • 相关阅读:
    sql语句中字符串分解查询的一种解决方法。
    VMware虚拟机的网络连接
    sql注入
    mvnrepository.com jar包下载
    局部刷新与json
    初涉json
    ios UIKit 基础控件创建与属性
    实用数学函数
    OC中的随机数函数——arc4random()
    OC中关于字符串的操作
  • 原文地址:https://www.cnblogs.com/ZMiOS/p/4924244.html
Copyright © 2011-2022 走看看