iOS中的NSTimer 和 Android 中的Timer

首先看iOS的,html

Scheduling Timers in Run Loops

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:ios

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.this


 

总结如下,在ios中,一个timer是和一个runloop密切相关的,用timer,就必须设定它的runloop,否则timer是没法正常工做的。另一个timer invalid以后,就没法再次启用,必须新建timer。spa

在iOS中,如不采用特殊设置,在应用程序进入后台后,与应用程序相关的线程当即暂停,天然在thread中执行的timer也会中止运行。当应用程序再次进入前台时,暂停的thread会被恢复。线程

另外须要注意,看下代码code

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0), ^{
        NSLog(@"async....");
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];


        timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:nil repeats:YES];

        [runloop addTimer:timer forMode:NSRunLoopCommonModes];
     //注意顺序,先加入源,再用run方法!
        [runloop run];


    });

新线程若是想启动runloop,不能单单写[runloop run],必须先加入一个触发源,好比这里的timer,否则runloop运行run方法后会当即返回,什么做用都没有。htm


 

在Android中,

Timer 在使用时自动开启新线程,好比如下代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

     System.out.println("Thread.currentThread()....."+Thread.currentThread().getId())  ;

        TimerTask task = new TimerTask() {
            public void run() {
                //每次须要执行的代码放到这里面。
                System.out.println("Thread.currentThread()"+Thread.currentThread().getId())  ;
                System.out.println(".........TimerTask.........run");

            }
        };



        Timer timer = new Timer();
        timer.schedule(task,1000,1000);
}

如下是输出

Thread.currentThread().....1
Thread.currentThread()319
Thread.currentThread()319

从这里咱们能够看出,Android中的timer是自动建立新线程并运行的,主线程的阻塞不会影响定时器的运行。

当程序进入后台后,程序的全部线程都不会中止,直到该线程被系统或代码中止。

相关文章
相关标签/搜索