Translator's note: I personally think that using timed tasks to run garbage collection is not a good example. Judging from the projects that translators come into contact with, it is more common to use timed tasks to perform non-real-time calculations and clear temporary data, files, etc.
In this article, I will introduce 3 different implementation methods:
1. Ordinary thread implementation
2. TimerTask implementation
3. ScheduledExecutorService implementation
1. Ordinary thread
This is the most common thing. Create a thread and then run it in the while loop, and use the sleep method to achieve the effect of the timing task. This can be implemented quickly and easily, the code is as follows:
The code copy is as follows:
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// -------- code for task to run
System.out.println("Hello !!");
// ---------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
2. Use Timer and TimerTask
The above implementation is very fast and easy, but it also lacks some functionality.
Compared with the above method, using Timer and TimerTask has the following benefits:
1. Controlled when starting and canceling tasks
2. You can specify the delay time you want when executing a task for the first time
During implementation, the Timer class can schedule tasks, and TimerTask implements specific tasks in the run() method.
Timer instances can schedule multitasking, and it is thread-safe.
When the Timer's constructor is called, it creates a thread that can be used to schedule tasks.
Here is the code:
The code copy is as follows:
import java.util.Timer;
import java.util.TimerTask;
public class Task2 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
@Override
public void run() {
// task to run goes here
System.out.println("Hello!!!");
}
};
Timer timer = new Timer();
long delay = 0;
long periodPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
integerPeriod);
} // end of main
}
These classes exist since JDK 1.3.
3. ScheduledExecutorService
ScheduledExecutorService was introduced as a concurrent tool class from Java SE 5 java.util.concurrent, which is the most ideal timed task implementation method.
Compared with the above two methods, it has the following benefits:
1. Compared with Timer's single thread, it executes tasks through a thread pool.
2. You can set the delay time for the first task execution
3. Provide good agreements to set the execution time interval
The following is the implementation code. We show this example through ScheduledExecutorService#scheduleAtFixedRate. Through the control of parameters in the code, the delay time is added for the first execution.
The code copy is as follows:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}