iOS中的NSTimer 和 Android 中的Timer

首先看iOS的,

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:

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.


 

总结以下,在ios中,一个timer是和一个runloop密切相关的,用timer,就必须设定它的runloop,不然timer是无法正常工作的。另外一个timer invalid之后,就无法再次启用,必须新建timer。

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

另外需要注意,看下代码

 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方法后会立即返回,什么作用都没有。


 

在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是自动创建新线程并运行的,主线程的阻塞不会影响定时器的运行。

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

 

posted @ 2014-08-28 11:47  幻化成疯  阅读(513)  评论(0编辑  收藏  举报