Package schedule;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;
Import Java.util.concurrent.TimeUnit;
/**
*
*
* Scheduledexecutorservice is from the Java SE5 java.util.concurrent, as a concurrent tool class was introduced, this is the most ideal timing task implementation method.
* 1> is a thread pool way to perform tasks compared to a single thread of a timer
* 2> can be flexible to set the first time to execute a task delay
* 3> provides a good convention to set the time interval for execution
*
* Below is the implementation code, we show this example through Scheduledexecutorservice#scheduleatfixedrate, through the control of the parameters in the code, the first execution added delay time.
*
*
* @author SHM
*
*/
public class Task {
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 ();
The second parameter is the delay time for the first execution, and the third parameter is the time interval for the scheduled execution
Service.scheduleatfixedrate (runnable, 1, timeunit.seconds);
}
}
Java implementation timed tasks