JAVAutil。Java中的计时器类

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

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

施工人员:

  • 计时器() :创建一个新计时器
  • 计时器(布尔isDaemon) :创建一个新的计时器,其关联的线程可以指定为作为守护进程运行
  • 计时器(字符串名称) :创建其关联线程具有指定名称的新计时器
  • 计时器(字符串名,布尔isDaemon) :创建一个新的计时器,其关联的线程具有指定的名称,并且可以指定作为守护进程运行

宣言:

public class Timer        extends Object

从java类继承的方法。lang.反对

  • 克隆
  • 等于
  • 定稿
  • getClass
  • 哈希码
  • 通知
  • 通知所有人
  • 托斯特林
  • 等待

方法:

  • cancel():java。util。计时器。取消 终止此计时器,放弃所有当前计划的任务。不会干扰当前正在执行的任务(如果存在)。一旦计时器被终止,它的执行线程就会优雅地终止,并且不能在其上安排更多的任务 语法:
public void cancel()
  • purge():java。util。计时器。清除() 从此计时器的任务队列中删除所有已取消的任务 语法:
public int purge()Returns:the number of tasks removed from the queue
  • 时间表(TimerTask任务,日期时间):java。util。计时器。计划(时间任务、日期时间) 计划在指定时间执行指定任务 语法:
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
  • 时间表(TimerTask任务、首次日期、长周期):java。util。计时器。时间表(TimerTask任务、首次日期、长周期) 从指定的时间开始,为重复的固定延迟执行计划指定的任务 语法:
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 <= 0IllegalStateException - 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 1Timer ran 2Timer ran 3Timer ran 4Timer ran 5...
  • 调度(TimerTask任务,长延迟):java。util。计时器。时间表(TimerTask任务,长延迟) 计划在指定延迟后执行指定的任务 语法:
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
  • 时间表(TimerTask任务、长延迟、长周期):java。util。计时器。时间表(TimerTask任务、长延迟、长周期) 将指定的任务安排为重复的固定延迟执行,从指定的延迟之后开始 语法:
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 <= 0IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.NullPointerException - if task is null
  • scheduleAtFixedRate(TimerTask任务,日期第一次,长周期):java。util。计时器。scheduleAtFixedRate(TimerTask任务、首次日期、长周期) 从指定的时间开始,为重复的固定速率执行计划指定的任务 语法:
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 <= 0IllegalStateException - 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。计时器。scheduleAtFixedRate(TimerTask任务、长延迟、长周期) 将指定的任务安排为重复的固定速率执行,从指定的延迟后开始 语法:
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 <= 0IllegalStateException - 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 runningTimer ran 1Timer ran 2Timer ran 3Timer ran 40

参考:

本文由 马扬克·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享