Two ScheduledExecutorService methods,
During development, other code is often executed in another thread, and implemented using the java scheduled task interface ScheduledExecutorService.
ScheduledExecutorService is a scheduled task class designed based on the thread pool. Each scheduling task is assigned to a thread in the thread pool for execution. That is to say, the task is executed concurrently without affecting each other.
Note: Only when the scheduling task comes, ScheduledExecutorService will actually start a thread, and ScheduledExecutorService will be in the polling task status for the rest of the time.
1. scheduleAtFixedRate Method
Example:
Import java. text. simpleDateFormat; import java. util. date; import java. util. concurrent. executors; import java. util. concurrent. scheduledExecutorService; import java. util. concurrent. timeUnit; public class ScheduleAtFixedRateDemo {public static void main (String [] args) {ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format executorService. scheduleAtFixedRate (new Runnable () {@ Override public void run () {System. out. println ("++ thread:" + df. format (new Date () ;}, 2, 3, TimeUnit. SECONDS); System. out. println ("++ main:" + df. format (new Date ()));}}
Running result:
++ Main: 15:20:52
++ Thread: 15:20:54
++ Thread: 15:20:57
++ Thread: 15:21:00
++ Thread: 15:21:03
++ Thread: 15:21:06
It can be seen that after 2 s, the sub-thread starts to execute and runs every 3 s round robin.
2. scheduleWithFixedDelay Method
Example:
Import java. text. simpleDateFormat; import java. util. date; import java. util. concurrent. executors; import java. util. concurrent. scheduledExecutorService; import java. util. concurrent. timeUnit;/*** ScheduleWithFixedDelay usage * @ author Administrator **/public class ScheduleWithFixedDelayDemo {public static void main (String [] args) {ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format executorService. scheduleWithFixedDelay (new Runnable () {@ Override public void run () {System. out. println ("++ thread:" + df. format (new Date () ;}, 2, 3, TimeUnit. SECONDS); System. out. println ("++ main:" + df. format (new Date ()));}}
Running result:
++ Main: 15:24:32
++ Thread: 15:24:34
++ Thread: 15:24:37
++ Thread: 15:24:40
++ Thread: 15:24:43
3. Two differences
ScheduleAtFixedRate each execution time is the interval from the start of the previous task to push back, that is, the execution time is initialDelay, initialDelay + period, initialDelay + 2 * period .....
ScheduleWithFixedDelay each execution time is the interval from the end of the last task to push back, that is, the execution time is: initialDelay, initialDelay + executeTime + delay, initialDelay + 2 * executeTime + 2 * delay .....
It can be seen that ScheduleAtFixedRate is based on a fixed interval for task scheduling, ScheduleWithFixedDelay depends on the duration of each task execution, and is based on a non-fixed interval for task scheduling.