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中文網其他相關文章!