zoukankan      html  css  js  c++  java
  • ios-NSRunLoop以及定时器NSTimer-理解

      [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"liyang" repeats:NO];
           NSTimer *tt= [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"limengxia" repeats:NO];
            [[NSRunLoop currentRunLoop]addTimer:tt forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop]run];
    //NSRunLoop的理解,每一个线程都有一个NSRunLoop对象,然而定时器也是在这个对象上面运行的,当一个线程运行完成了过后,会自动关闭线程,自然NSRunLoop也会被销毁,自然定时器就不会运行,为了不让其线程关闭,用此语句   
    [[NSRunLoop currentRunLoop]run];那么线程就会保持活跃状态(前提是这个线程里面还有需要被执行的东西,比如说定时器,网络请求等(经过测试的,网络请求,GCD也一样),(这可能是一种优化,单凭这句话,还不能将此线程保持活跃,必须有需要执行的东西)),不会被关闭,自然定时器也就能用了,
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"liyang" repeats:NO];这么设置的定时器是自动添加到了NSRunLoop中的,
    NSTimer *tt= [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"limengxia" repeats:NO];这个定时器需要手动添加
    [[NSRunLoop currentRunLoop]addTimer:tt forMode:NSDefaultRunLoopMode];

     如果要使定时器精确,最好将这个定时器放到多线程上去,防止线程阻塞,影响定时器的精确度

     CFRunLoopRun();
        CFRunLoopStop(CFRunLoopGetCurrent());
    //最好是成对的出现,这个和上面的维持runloop活跃状态和关掉时一样的,要不然多线程一执行完成就会关掉,这样代理那些就执行不了
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    js如何将字符串作为函数名调用函数
    js如何生成[n,m]的随机数
    UIMenuController,UIPasteboard:复制,粘贴详细解释
    python2.7和 python3.4但是不要
    Android IPC通信和AIDL技术应用
    可穿戴式智能设备,其潜在的安全问题?(上)
    CentOS安装KVM步骤虚拟机,绝对实用!
    Python于*args 和**kwargs使用
    uva 1556
    JSCover+WebDriver/Selenium获得JS 代码覆盖
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3662557.html
Copyright © 2011-2022 走看看