首页 > Java > java教程 > Java 中的定时器

Java 中的定时器

PHPz
发布: 2024-08-30 15:53:07
原创
457 人浏览过

Java 中的定时器在 java 中可用。 util 包扩展了 Object 类并实现了 Serialized 和 Cloneable 接口。计时器类包含用于执行与计时相关的活动的方法。 Java中的Timer类用于执行与时间相关的任务调度。 Java 线程使用 Timer 类的方法来安排任务,例如在某个时刻后执行一段代码,在某个预定义的时间后重复执行代码。每个 Timer 对象都绑定到一个单独的后台运行线程,该线程负责执行与该线程关联的所有任务。需要注意的是,java中的定时器类是线程安全的;也就是说,在某一时刻,只有一个线程可以执行Timer类的方法。 Timer 类还使用二进制堆作为底层数据结构来存储任务。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java 中定时器的语法

这是在 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类的方法

现在我们将看到 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.
方法名称 描述 public void 计划(TimerTask 任务,Date 日期) 安排任务在定义的日期执行。 公共无效计划(TimerTask 任务、日期firstTime、长时间段) 第一个参数是要执行的TimerTask;第二个参数是第一次执行任务的时间,第三个参数是秒(以毫秒为单位),之后任务将定期执行。 public int purge() 用于从计时器队列中删除所有已取消的任务。 公共无效取消() 取消计时器。 public void Schedule(TimeTask任务,延时较长) 安排任务在指定时间(以毫秒为单位)后执行。 公共无效日程(TimeTask任务,长延迟,长周期) 第一个参数是要执行的TimerTask;第二个参数是第一次执行任务的时间(以毫秒为单位),第三个参数是定期执行任务的秒数(以毫秒为单位)。 public  void ScheduleAtFixedRate(TimerTask 任务,日期firstTime,长时间段) 第一个参数是要执行的TimerTask;第二个参数是第一次执行任务的时间,第三个参数是秒(以毫秒为单位),之后定期执行任务。 public void ScheduleAtFixedRate(TimeTask任务,长延迟,长周期) 第一个参数是要执行的TimerTask;第二个参数是第一次执行任务的时间(以毫秒为单位),第三个参数是定期执行任务的秒数(以毫秒为单位)。 表>

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.

Constructors in Timer Class

The timer class contains four constructors for instantiating timer object.

  • Timer(): Creates a new Timer Object.
  • Timer(boolean isDaemon): Creates a timer object with a corresponding thread specified to run as a daemon.
  • Timer(String name): Creates a timer object with a corresponding thread name.
  • Timer(String name, boolean isDaemon): This method is a combination of the above two constructors.

One of the above four listed constructors can be called depending on our requirements.

Examples of Implementing Timer in Java

Below is the example of Timer in Java:

Example #1

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:

Java 中的定时器

Example #2

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 中的定时器

以上是Java 中的定时器的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板