Explanation of ScheduleAtFixedRate and ScheduleWithFixedDelay methods in ScheduledThreadPoolExecutor

The ScheduledExecutorService interface in java is a scheduled task class designed based on the thread pool. Each scheduled task will be assigned to a thread in the thread pool for execution. That is to say, the tasks are executed concurrently without affecting each other.
One of the implementation classes is ScheduledThreadPoolExecutor. The uml class diagram relationship of ScheduledThreadPoolExecutor is as follows:

Write picture description here

(1)>ScheduledThreadPoolExecutor implements the ScheduledExecutorService interface and implements some timing task processing methods.

(2)>ScheduledThreadPoolExecutor inherits ThreadPoolExecutor, and can manage and schedule tasks through the thread pool.

The following introduces the two most commonly used scheduling methods for implementing the ScheduledExecutorService interface in ScheduledThreadPoolExecutor, ScheduleAtFixedRate and ScheduleWithFixedDelay.

1. scheduleAtFixedRate Method
1: Method Introduction

scheduleAtFixedRate(Runnable command,
                                long initialDelay,
                                  long period,
                                  TimeUnit unit)

The above four parameters are explained:
the first command parameter is the task instance,
the second initialDelay parameter is the initialization delay time,
the third period parameter is the interval time, and
the fourth unit parameter is the time unit.

2: Code example
(1): When the execution time of the task instance commod is less than the interval time period

public class TestExecutor {
    
    

    private static  ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);

    public static void main(String[] args) {
    
    


    }

    /**
     *  进行scheduleAtFixedRate测试
     */
    public static void testFixedRate(){
    
    
        executor.scheduleAtFixedRate(new myRun(), 5, 5, TimeUnit.SECONDS);
    }


    static class myRun implements Runnable{
    
    

        @Override
        public void run() {
    
    
            System.out.println("----测试开始--------"+ new Date().toLocaleString());
            try {
    
    
                Thread.sleep(3000);
            } catch (InterruptedException e) {
    
    
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("---休眠3秒后, 处理结束--------"+new Date().toLocaleString());
        }
    }

}

operation result:

----测试开始--------2017-10-11 11:38:38  #第一次执行
---休眠3秒后, 处理结束--------2017-10-11 11:38:41  #第一次任务处理,花费3秒
 #第二次执行时间是第一次时间 + period 即38 + 5 = 43----测试开始--------2017-10-11 11:38:43 
---休眠3秒后, 处理结束--------2017-10-11 11:38:46
----测试开始--------2017-10-11 11:38:48
---休眠3秒后, 处理结束--------2017-10-11 11:38:51
----测试开始--------2017-10-11 11:38:53

(2): When the execution time of the task instance commod is longer than the interval time period,
modify Thread.sleep(3000); to Thread.sleep(6000); and execute to view the running results!

operation result:

----测试开始--------2017-10-11 11:41:22 #第一次执行时间
---休眠3秒后, 处理结束--------2017-10-11 11:41:28 # 任务处理6秒,即 22+6 = 28
#第二次执行时间 == 上一次处理结束时间,因为任务处理时间大于period间隔时间
----测试开始--------2017-10-11 11:41:28 
---休眠3秒后, 处理结束--------2017-10-11 11:41:34
----测试开始--------2017-10-11 11:41:34

3: Summary
ScheduleAtFixedRate Each execution time is a time interval backwards from the start of the last task. Divided into two cases:
(1) If the execution time of the command is less than the period, if each execution time is: initialDelay, initialDelay+period, initialDelay+2*period, ...;

(2) If the execution time of the command is greater than the period, the command is executed, and the next task will be executed immediately! The following means that the next task will not be executed according to the expected time interval, each execution time is: initialDelay, initialDelay+taskExecutorTIme, initialDelay+2*taskExecutorTIme, ...;

taskExecutorTIme is the execution time of the task!

二、scheduleWithFixedDelay

1: Introduction to the method

scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit

The above four parameters are explained:
the first command parameter is the task instance,
the second initialDelay parameter is the initial delay time,
the third delay parameter is the delay interval time, and
the fourth unit parameter is the time unit

2: Code example

(1): When the execution time of the task instance commod is less than the delay interval time delay

public class TestExecutor {
    
    

    private static  ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);

    public static void main(String[] args) {
    
    
        testFixedDelay();

    }




    public static void testFixedDelay(){
    
    
        executor.scheduleWithFixedDelay(new myRun(), 5, 5, TimeUnit.SECONDS);
    }


    static class myRun implements Runnable{
    
    

        @Override
        public void run() {
    
    
            System.out.println("----测试开始--------"+ new Date().toLocaleString());
            try {
    
    
                Thread.sleep(3000);
            } catch (InterruptedException e) {
    
    
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("---休眠3秒后, 处理结束--------"+new Date().toLocaleString());
        }
    }


}

operation result:

----测试开始--------2017-10-11 11:59:02 #第一次执行时间
---休眠3秒后, 处理结束--------2017-10-11 11:59:05 #任务处理的时间,3秒
#第二次执行的时间 == 第一次任务开始时间+任务处理时间+delay延迟时间
#即 10 == 02 + 3+  5----测试开始--------2017-10-11 11:59:10
---休眠3秒后, 处理结束--------2017-10-11 11:59:13
----测试开始--------2017-10-11 11:59:18
---休眠3秒后, 处理结束--------2017-10-11 11:59:21

(2): When the execution time of the task instance commod is greater than the delay interval delay,
change Thread.sleep(3000); to Thread.sleep(6000);
and the running result:

----测试开始--------2017-10-11 12:02:48 #第一次任务执行开始时间
---休眠6秒后, 处理结束--------2017-10-11 12:02:54  #任务处理的时间 ,6秒
#第二次任务执行开始时间 == 第一次任务执行开始时间 + 任务处理的时间 + delay延迟时间
#即 59 == 48 + 6 + 5 
----测试开始--------2017-10-11 12:02:59
---休眠6秒后, 处理结束--------2017-10-11 12:03:05
----测试开始--------2017-10-11 12:03:10

3: Summary
No matter how long the execution time of the task command is, the execution time of the next task is to execute the next task after waiting for the delay interval delay time after the execution of the previous task.
Each execution time of ScheduleWithFixedDelay is pushed back by a time interval from the end of the last task, that is, each execution time is: initialDelay, initialDelay+executeTime+delay, initialDelay+2 executeTime +2 delay.

Reference
https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/130172820