📜  Java.util.Timer 类在Java

📅  最后修改于: 2022-05-13 01:55:25.725000             🧑  作者: Mango

Java.util.Timer 类在Java

Timer 类提供了一个方法调用,线程使用该方法调用来调度任务,例如在某个固定时间点之后运行代码块。每个任务可以安排运行一次或重复执行次数。每个计时器对象都与一个后台线程相关联,该线程负责执行计时器对象的所有任务。
笔记:

  • Timer 类是线程安全的。
  • Timer 类使用二进制堆数据结构来存储其任务。

构造函数:

  • Timer() : 创建一个新的计时器
  • Timer(boolean isDaemon) : 创建一个新的定时器,它的关联线程可以被指定为守护进程运行
  • Timer(String name) : 创建一个新的定时器,其关联线程具有指定的名称
  • Timer(String name, boolean isDaemon) :创建一个新的计时器,其关联线程具有指定的名称,并且可以指定为作为守护进程运行

宣言:

public class Timer
        extends Object

从类Java.lang.Object 继承的方法

  • 克隆
  • 等于
  • 敲定
  • 获取类
  • 哈希码
  • 通知
  • 通知所有
  • 到字符串
  • 等待

方法:

  • cancel(): Java.util.Timer.cancel()终止这个定时器,丢弃任何当前计划的任务。不干扰当前正在执行的任务(如果存在)。一旦定时器被终止,它的执行线程就会优雅地终止,并且不能在其上安排更多的任务
    句法:
public void cancel()
  • purge(): Java.util.Timer.purge()从此计时器的任务队列中删除所有取消的任务
    句法:
public int purge()
Returns:
the number of tasks removed from the queue
  • schedule(TimerTask task, Date time): Java.util.Timer.schedule(TimerTask task, Date time)安排指定任务在指定时间执行
    句法:
public void schedule(TimerTask task, Date time)
Parameters:
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
  • schedule(TimerTask task, Date firstTime, long period): Java.util.Timer.schedule(TimerTask task, Date firstTime, long period)安排指定任务重复固定延迟执行,从指定时间开始
    句法:
public void schedule(TimerTask task, Date firstTime, long period)
Parameters:
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);
         
    }
}


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 ran 1
Timer ran 2
Timer ran 3
Timer ran 4
Timer ran 5
.
.
.
  • schedule(TimerTask task, long delay): Java.util.Timer.schedule(TimerTask task, long delay)安排指定任务在指定延迟后执行
    句法:
public void schedule(TimerTask task, long delay)
Parameters:
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 task, long delay, long period): Java.util.Timer.schedule(TimerTask task, long delay, long period)安排指定任务重复固定延迟执行,在指定延迟后开始语法:
public void schedule(TimerTask task, long delay, long period)
Parameters:
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 task, Date firstTime, long period): Java.util.Timer.scheduleAtFixedRate(TimerTask task, Date firstTime, long period)调度指定任务以重复固定速率执行,从指定时间开始语法:
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Parameters:
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 task, long delay, long period): Java.util.Timer.scheduleAtFixedRate(TimerTask task, long delay, long period)调度指定任务以重复固定速率执行,在指定延迟之后开始语法:
public void scheduleAtFixedRate(TimerTask task, long delay, long period)
Parameters:
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

参考:

  • 甲骨文