首页 > 代码库 > iOS中的NSTimer 和 Android 中的Timer

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:

  • Use the scheduledTimerWithTimeInterval:invocation:repeats: orscheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

  • Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

  • Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoopobject.)

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];        [runloop run];    });

新线程如果想启动runloop,不能单单写[runloop run],必须加入一个触发源,比如这里的timer,不然runloop是不能启动的。


 

在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().....1Thread.currentThread()319Thread.currentThread()319

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

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

 

iOS中的NSTimer 和 Android 中的Timer