timer schudele的使用
本次使用主要圍繞着以下即方面開展:
- java api介紹
- schedule在任務調度上的使用
- 任務的開啓和取消
- 任務執行過程中的異常情況
java API的介紹
timer 構造器 |
---|
Timer() Creates a new timer. |
Timer(boolean isDaemon) Creates a new timer whose associated thread may be specified to run as a daemon. |
Timer(String name) Creates a new timer whose associated thread has the specified name. |
Timer(String name, boolean isDaemon) Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon. |
timer 的方法 | |
---|---|
Modifier and Type | Method and Description |
void | cancel() Terminates this timer, discarding any currently scheduled tasks. |
int | purge() Removes all cancelled tasks from this timer’s task queue. |
void | schedule(TimerTask task, Date time) Schedules the specified task for execution at the specified time. |
void | schedule(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. |
void | schedule(TimerTask task, long delay) Schedules the specified task for execution after the specified delay. |
void | schedule(TimerTask task, long delay, long period) Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. |
void | scheduleAtFixedRate(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. |
void | scheduleAtFixedRate(TimerTask task, long delay, long period) Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. |
除了以上方法以外,還繼承了對象的一些方法,如下
繼承對象的方法 |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
以上,這些方法中,主要要明白,該類具有什麼樣的功能,什麼時候使用該類。任務管理方法的使用,同時該方法定義的任務都是在一個獨立的線程中運行。
下面再介紹與該任務管理器有關的timer Task類。
如果你要定義一個定時任務,只需要將你的任務類繼承timerTask類就行 了,執行的任務主要在run()方法進行實現。timerTask類繼承了runnable類,因此是線程安全的。