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类,因此是线程安全的。