Java 中的定时器在 java 中可用。 util 包扩展了 Object 类并实现了 Serialized 和 Cloneable 接口。计时器类包含用于执行与计时相关的活动的方法。 Java中的Timer类用于执行与时间相关的任务调度。 Java 线程使用 Timer 类的方法来安排任务,例如在某个时刻后执行一段代码,在某个预定义的时间后重复执行代码。每个 Timer 对象都绑定到一个单独的后台运行线程,该线程负责执行与该线程关联的所有任务。需要注意的是,java中的定时器类是线程安全的;也就是说,在某一时刻,只有一个线程可以执行Timer类的方法。 Timer 类还使用二进制堆作为底层数据结构来存储任务。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
这是在 java 中如何使用 Timer 类的基本语法:
语法:
// create a class extending TimerTask class TimerHelper extends TimerTask { //define run method public void run() { // Write Code to be executed by Timer } } class MainClass{ public static void main(String[] args) { //create timer instance Timer timer = new Timer(); // create Timer class instance TimerTask task = new TimerHelper (); // call timer method timer.schedule(task, 3000,6000); //first argument is timer object // second argument is time in milliseconds after which the code will be first executed // Third argument is time in milliseconds after which the code will be executed regularly. } }
上述语法的解释:该语法展示了如何在java中使用Timer类。使用计时器类涉及创建一个扩展 TimerTask 的类并在其中定义 run 方法。 run 方法包含需要在时间驱动的基础上执行的逻辑。以下是 Timer 类声明:
public class Timer extends Object implements Serializable, Cloneable
现在我们将看到 java Timer 类中可用的不同方法和字段。以下是 Timer 类中常用方法的列表:
Method Name | Description |
public void schedule(TimerTask task, Date date) | Schedules a task to be executed on the defined date. |
public void schedule (TimerTask task, Date firstTime, long timeperiod) | The first argument is TimerTask to be executed; the second argument is the time after which the task is executed for the first time, and the third argument is seconds in milliseconds after which task will be executed regularly. |
public int purge() | Used for removing all canceled tasks from the timer’s queue. |
public void cancel() | Cancel’s the timer. |
public void schedule(TimeTask task, long delay) | Schedules the task to be executed after the specified time in milliseconds. |
public void schedule(TimeTask task, long delay, long period) | The first argument is TimerTask to be executed; the second argument is the time in milliseconds after which task is executed for the first time, and the third argument is seconds in milliseconds after which task will be executed regularly. |
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long timeperiod) | The first argument is TimerTask to be executed; the second argument is the time after which the task is executed for the first time, and the third argument is seconds in milliseconds after which the task will be executed regularly. |
public void scheduleAtFixedRate (TimeTask task, long delay, long period) | The first argument is TimerTask to be executed; the second argument is the time in milliseconds after which task is executed for the first time, and the third argument is seconds in milliseconds after which task will be executed regularly. |
From the above-stated methods, we have found two methods that are similar in working but different in the name; they are schedule and scheduleAtFixedRate. The difference between the two is that in the case of fixed-rate execution, each execution is scheduled in accordance with the initial execution. If there is a delay in execution, then two or more executions will occur in quick succession to overcome the delay.
The timer class contains four constructors for instantiating timer object.
One of the above four listed constructors can be called depending on our requirements.
Below is the example of Timer in Java:
To start things, let us see a basic example of Timer class. In this example, we will demonstrate the use of the schedule method of the Timer class.
Code:
package com.edubca.timer; import java.util.Timer; import java.util.TimerTask; class TimerHelper extends TimerTask { public static int counter = 0; public void run() { counter++; System.out.println("Timer run Number " + counter); } } public class Main { public static void main(String[] args) { Timer timer = new Timer(); TimerTask timerhelper = new TimerHelper(); timer.schedule(timerhelper, 3000, 2000); } }
Explanation of the above code: The above code will execute the run method for the first time after 3 seconds as the first argument is 3000, and after every 2 seconds, the run method will be executed regularly. Here is the output that will be displayed:
Output:
In this example, we will see how to terminate a timer thread after a given number of timer runs.
Code:
package com.edubca.timer; import java.util.Timer; import java.util.TimerTask; class TimerHelper extends TimerTask { public static int counter = 0; public void run() { counter++; if(counter ==3){ this.cancel(); System.out.println("Now Cancelling Thread!!!!!"); return; } System.out.println("Timer run Number " + counter); } } public class Demo { public static void main(String[] args) { Timer timer = new Timer(); TimerTask helper = new TimerHelper(); helper.schedule(task, 3000, 2000); } }
In the above example, the timer will cancel after the three times run method is called using the timer class’s cancel method.
Output:
以上是Java 中的定时器的详细内容。更多信息请关注PHP中文网其他相关文章!