Timer 類提供了一個方法調用,線程使用該方法調用來安排任務,例如在某個常規時間點後運行一段代碼。每個任務可以安排運行一次或重複執行多次。每個計時器對象都與一個後台線程關聯,該後台線程負責執行計時器對象的所有任務。
筆記:
- Timer 類是線程安全的。
- Timer 類使用二進製堆數據結構來存儲其任務。
構造函數:
- Timer():創建一個新的計時器
- 計時器(布爾 isDaemon):創建一個新的計時器,其關聯的線程可以指定為守護進程運行
- 計時器(字符串名稱):創建一個新的計時器,其關聯的線程具有指定的名稱
- 計時器(字符串名稱,布爾值 isDaemon):創建一個新的計時器,其關聯的線程具有指定的名稱,並且可以指定為作為守護進程運行
聲明:
public class Timer extends Object
從類 java.lang.Object 繼承的方法
- clone
- equals
- finalize
- getClass
- hashCode
- notify
- notifyAll
- toString
- wait
方法:
- cancel(): java.util.Timer.cancel()終止此計時器,丟棄所有當前計劃的任務。不幹擾當前正在執行的任務(如果存在)。一旦計時器終止,其執行線程就會正常終止,並且不能在其上調度更多任務
用法:
public void cancel()
- purge(): java.util.Timer.purge()從此計時器的任務隊列中刪除所有已取消的任務
用法:
public int purge() 返回: the number of tasks removed from the queue
- 時間表(TimerTask任務,日期時間):java.util.Timer.schedule(TimerTask任務,日期時間)安排指定的任務在指定的時間執行
用法:
public void schedule(TimerTask task, Date time) 參數: task - task to be scheduled. time - time at which task is to be executed. Throws: IllegalArgumentException - if time.getTime() is negative. IllegalStateException - if the task was already scheduled or cancelled, the timer was cancelled, or timer thread terminated. NullPointerException - if task or time is null
- 時間表(TimerTask任務,日期firstTime,長周期):java.util.Timer.schedule(TimerTask任務,日期firstTime,長周期)安排指定任務從指定時間開始重複執行fixed-delay
用法:
public void schedule(TimerTask task, Date firstTime, long period) 參數: task - task to be scheduled. firstTime - First time at which task is to be executed. period - time in milliseconds between successive task executions. Throws: IllegalArgumentException - if firstTime.getTime() < 0, or period <= 0 IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. NullPointerException - if task or firstTime is null
Java
// Java program to demonstrate
//schedule method calls of Timer class
import java.util.Timer;
import java.util.TimerTask;
class Helper extends TimerTask
{
public static int i = 0;
public void run()
{
System.out.println("Timer ran " + ++i);
}
}
public class Test
{
public static void main(String[] args)
{
Timer timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 2000, 5000);
}
}
輸出:
Timer ran 1 Timer ran 2 Timer ran 3 Timer ran 4 Timer ran 5 . . .
- Schedule(TimerTask任務,長延遲):java.util.Timer.schedule(TimerTask任務,長延遲)安排指定的任務在指定的延遲後執行
用法:
public void schedule(TimerTask task, long delay) 參數: task - task to be scheduled. delay - delay in milliseconds before task is to be executed. Throws: IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative. IllegalStateException - if a task was already scheduled or cancelled, the timer was cancelled, or timer thread terminated. NullPointerException - if task is null
- Schedule(TimerTask任務,長延遲,長周期):java.util.Timer.schedule(TimerTask任務,長延遲,長周期)安排指定的任務重複執行fixed-delay,在指定的延遲後開始用法:
public void schedule(TimerTask task, long delay, long period) 參數: task - task to be scheduled. delay - delay in milliseconds before task is to be executed. period - time in milliseconds between successive task executions. Throws: IllegalArgumentException - if delay < 0, or delay + System.currentTimeMillis() < 0, or period <= 0 IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. NullPointerException - if task is null
- ScheduleAtFixedRate(TimerTask任務,日期firstTime,長周期):java.util.Timer.scheduleAtFixedRate(TimerTask任務,日期firstTime,長周期)安排指定任務從指定時間開始重複執行fixed-rate用法:
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) 參數: task - task to be scheduled. firstTime - First time at which task is to be executed. period - time in milliseconds between successive task executions. Throws: IllegalArgumentException - if firstTime.getTime() < 0 or period <= 0 IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. NullPointerException - if task or firstTime is null
- ScheduleAtFixedRate(TimerTask任務,長延遲,長周期):java.util.Timer.scheduleAtFixedRate(TimerTask任務,長延遲,長周期)安排指定的任務重複執行fixed-rate,在指定的延遲後開始用法:
public void scheduleAtFixedRate(TimerTask task, long delay, long period) 參數: task - task to be scheduled. delay - delay in milliseconds before task is to be executed. period - time in milliseconds between successive task executions. Throws: IllegalArgumentException - if delay < 0, or delay + System.currentTimeMillis() < 0, or period <= 0 IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. NullPointerException - if task is null
Java
// Java program to demonstrate
// scheduleAtFixedRate method of Timer class
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
class Helper extends TimerTask
{
public static int i = 0;
public void run()
{
System.out.println("Timer ran " + ++i);
if(i == 4)
{
synchronized(Test.obj)
{
Test.obj.notify();
}
}
}
}
public class Test
{
protected static Test obj;
public static void main(String[] args) throws InterruptedException
{
obj = new Test();
//creating a new instance of timer class
Timer timer = new Timer();
TimerTask task = new Helper();
//instance of date object for fixed-rate execution
Date date = new Date();
timer.scheduleAtFixedRate(task, date, 5000);
System.out.println("Timer running");
synchronized(obj)
{
//make the main thread wait
obj.wait();
//once timer has scheduled the task 4 times,
//main thread resumes
//and terminates the timer
timer.cancel();
//purge is used to remove all cancelled
//tasks from the timer'stack queue
System.out.println(timer.purge());
}
}
}
輸出:
Timer running Timer ran 1 Timer ran 2 Timer ran 3 Timer ran 4 0
參考:
相關用法
- Java Java.util.TimerTask用法及代碼示例
- Java Java.util.ArrayDeque.add()用法及代碼示例
- Java Java.util.ArrayDeque.addFirst()用法及代碼示例
- Java Java.util.ArrayDeque.addLast()用法及代碼示例
- Java Java.util.ArrayDeque.clear()用法及代碼示例
- Java Java.util.ArrayDeque.clone()用法及代碼示例
- Java Java.util.ArrayDeque.descendingIterator()用法及代碼示例
- Java Java.util.ArrayDeque.element()用法及代碼示例
- Java Java.util.ArrayDeque.getFirst()用法及代碼示例
- Java Java.util.ArrayDeque.getLast()用法及代碼示例
- Java Java.util.ArrayDeque.isEmpty()用法及代碼示例
- Java Java.util.ArrayDeque.iterator()用法及代碼示例
- Java Java.util.ArrayDeque.peek()用法及代碼示例
- Java Java.util.ArrayDeque.peekFirst()用法及代碼示例
- Java Java.util.ArrayDeque.peekLast()用法及代碼示例
- Java Java.util.ArrayDeque.poll()用法及代碼示例
- Java Java.util.ArrayDeque.pollFirst()用法及代碼示例
- Java Java.util.ArrayDeque.pollLast()用法及代碼示例
- Java Java.util.ArrayDeque.pop()用法及代碼示例
- Java Java.util.ArrayDeque.push()用法及代碼示例
- Java Java.util.ArrayDeque.remove()用法及代碼示例
- Java Java.util.ArrayDeque.removeFirst()用法及代碼示例
- Java Java.util.ArrayDeque.removeLast()用法及代碼示例
- Java Java.util.ArrayDeque.removeLastOccurrence()用法及代碼示例
- Java Java.util.ArrayDeque.size()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.util.Timer Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。