Home > Java > javaTutorial > The use of timers in JAVA

The use of timers in JAVA

高洛峰
Release: 2016-12-16 11:58:38
Original
1213 people have browsed it

The two classes used to implement the timer function in JAVA are Timer and TimerTask
The Timer class is a class used to execute tasks. It accepts a TimerTask as a parameter
Timer has two modes for executing tasks, the most commonly used is schedule , it can perform tasks in two ways: 1: at a certain time (Data), 2: after a fixed time (int delay). Both methods can specify the frequency of task execution. This article has two Examples, one is simple
The other is using an internal class
1. Simple example

First write a class

public class TimeTest {
public static void main(String[] args) {
​​​​​
​​Timer timer = new Timer( );
timer.schedule(new MyTask(),1000,2000);
}

Then write a class
public class MyTask extends TimerTask{

@Override
public void run() {
System.out.println ("Start running");                                                                                                           . Internal class

public class SerchRun {

                                                                use   through ’ ’ s ’     through ’ through ’ through through use using ’ s through through ’s through ’ s through ’ through through through through ‐ through‐to‐‐‐‐‐‐ to System.out.println(" Start running"); //Write the method you want to call here
 / timer.scheduleAtFixedRate(task, 1000,2000); // Start after 1 second. Start again every 2 seconds.
}
}



The difference between schedule and scheduleAtFixedRate is that if the specified start execution time is before the current system running time, scheduleAtFixedRate will also execute the past time as a period, while schedule will not count the past time.

For example:

SimpleDateFormat fTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d1 = fTime.parse("2005/12/30 14:10:00");

t.scheduleAtFixedRate(new TimerTask(){
public void run()
{
System.out.println("this is task you do6");
}
},d1,3*60*1000);

interval The time is 3 minutes, and the specified start time is 2005/12/30 14:10:00. If I execute this program at 14:17:00, it will be printed three times immediately

this is task you do6 //14: 10

this is task you do6 //14:13

this is task you do6 //14:16

And note that the next execution is at 14:19 instead of 14:20. That is to say, timing starts from the specified start time, not from the execution time.


But if the schedule method is used above, the interval is 3 minutes, and the specified start time is 2005/12/30 14:10:00, then if the program is executed at 14:17:00, the program will be executed immediately. And the next execution time is 14:20, not the period starting from 14:10 (14:19).






For more articles related to the use of timers in JAVA, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template