This time, I will explain how to execute tasks regularly in Java. This function is often used in batch processing.
In the sample, we will use the Timer class to implement the process of executing tasks on a regular basis.
Then I will explain it immediately.
After that, it will be explained in the following versions and environments.
IDE:eclipse Java version: 8
The folder structure of this sample program is as follows.
Implement tasks that you want to execute on a regular basis using the TimerTask class. This time, we will implement a sample process that periodically outputs "The task has been executed" to the console.
Main.java
package main;
//The part to be added this time
import java.util.TimerTask;
public class Main {
public static void main(String[] args) {
//The part to be added this time
TimerTask task = new TimerTask() {
public void run() {
//Processing that you want to execute on a regular basis
System.out.println("The task has been executed.");
}
};
}
}
Also, in order to check how many times it has been executed, let's implement it so that you can see the number of times as follows.
Main.java
package main;
import java.util.TimerTask;
public class Main {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
int count = 0; //Processing to be added this time
public void run() {
//Processing that you want to execute on a regular basis
count++; //Processing to be added this time
System.out.println(count + "The second task has been performed."); //Processing to be corrected this time
}
};
}
}
Next, we will implement when to start executing the task and how often to execute the task.
As for the implementation method, use the scheduleAtFixedRate method of the Timer class. The usage is as follows.
Timer timer = new Timer();
timer.scheduleAtFixedRate(Tasks you want to perform on a regular basis,Time to execute the first task(ms),Interval between tasks to perform(ms));
Although it is the second argument of the scheduleAtFixedRate method, not only "time until the first task execution" but also "task start time" can be specified by Date type.
In this sample, we will implement the process that the task is executed at 3 second intervals 1 second after PGM is executed.
Main.java
package main;
import java.util.Timer; //Processing to be added this time
import java.util.TimerTask;
public class Main {
public static void main(String[] args) {
Timer timer = new Timer(); //Processing to be added this time
TimerTask task = new TimerTask() {
int count = 0;
public void run() {
//Processing that you want to execute on a regular basis
count++;
System.out.println(count + "The second task has been performed.");
}
};
timer.scheduleAtFixedRate(task,1000,3000); //Processing to be added this time
}
}
After completing the above implementation, right-click Main.java> Run Java Application to execute it.
It is OK if the following words are output to the console at 3 second intervals.
console
The first task has been performed.
The second task has been performed.
The third task has been performed.
The fourth task has been performed.
・ ・ ・
I started my personal blog in 2020!
Based on the knowledge and experience gained as a freelance engineer, we plan to distribute content such as information on freelance engineers, IT technical information, industry information, and engineer life hacks.
The number of articles is still small, but it is updated weekly, so if you are interested, I would be grateful if you could take a look.
https://yacchi-engineer.com/
Recommended Posts