Scheduledexecutorservice extends the Executorservice interface, providing the ability to perform tasks regularly (like a timer). API Description
| method
Desc |
Schedule (callable<v> callable, long delay, timeunit unit) |
Creates and executes a scheduledfuture that is enabled after a given delay. |
Schedule (Runnable command, long delay, timeunit unit) |
Creates and executes a one-time operation that is enabled after a given delay. |
Scheduleatfixedrate (Runnable command, long Initialdelay,long period, timeunitunit) |
Creates and executes a periodic operation that was first enabled after a given initial delay, with a given period of time; that is, the execution begins after InitialDelay, then executes after Initialdelay+period, followed by InitialDelay + 2 * Period After execution, and so forth. |
Schedulewithfixeddelay (Runnable command, Long initialdelay, long delay,timeunit unit) |
Creates and executes a periodic operation that was first enabled after a given initial delay, followed by a given delay between execution termination and the start of the next execution. |
The schedule method is used to delay the specified time to perform a specified task. If you need to perform recurring tasks periodically, you can use either the Scheduleatfixedrate or the Schedulewithfixeddelay method, which is different in that the former is performed at a fixed frequency and the latter at a relatively fixed frequency.
Scheduleatfixedrate and Schedulewithfixeddelay do not cause the same task to be executed concurrently, regardless of whether the task's execution time is greater than the interval.
If any execution the of this task takes longer than its period, then subsequent executions may start late, but would not concur rently Execute. Attention Matters
Periodic tasks performed through Scheduledexecutorservice, if an exception is thrown during the execution of a task, the Scheduledexecutorservice stops performing the task and the subsequent tasks are not executed.
If any execution to the task encounters an exception, subsequent executions are. Simple Example 1. Regular execution
Package Com.bytebeats.codelab;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;
Import Java.util.concurrent.TimeUnit;
/**
* ${description}
* *
@author Ricky Fung
* @create 2016-08-15
23:38/public class Scheduledexecutorservicedemo {public
static void Main (string[] args) throws Interruptedexception {
Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
Scheduledexecutorservice.schedule (New Runnable () {
@Override public
void Run () {
System.out.println ( "Count ...");
}
, 1, timeunit.seconds)
; TimeUnit.SECONDS.sleep (5);
Scheduledexecutorservice.shutdown ();
}
2, fixed frequency to perform the task
Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
Scheduledexecutorservice.scheduleatfixedrate (New Runnable () {
@Override public
void Run () {
System.out.println ("Count ...");
}
, 1, 2, timeunit.seconds);
TimeUnit.SECONDS.sleep (ten);
Scheduledexecutorservice.shutdown ();
3, fixed time interval to perform the task
Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
Scheduledexecutorservice.schedulewithfixeddelay (New Runnable () {
@Override public
void Run () {
System.out.println ("Count ...");
}
, 1, 1, timeunit.seconds);
reference materials
Https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html