Timer:A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.(使用線程來實現定時執行或者週期性執行任務的效果)
Timer thread as either a daemon thread or a user thread.Timer線程既可以當做守護線程來執行,也可以當做用戶線程來執行。默認爲用戶線程,可在構造函數中指定或者在創建後使用setDeamon來設置。
用戶線程和守護線程的區別:
1、用戶線程:不依賴於其他線程,可以自己單獨存在
2、守護線程:必須依賴用戶線程,即必須得在有一個或多個用戶線程存在的條件下,才能存在。如果用戶線程結束,則應用程序結束,那麼守護線程肯定也就結束了。
給Timer添加TimerTask(任務):
Timer.schedule(TimerTask task, long delay, long period)
TimerTask實現了Runnable接口