ScheduledThreadPoolExecutor 详解

table of Contents

One, the creation of ScheduledThreadPoolExecutor

Second, the realization of ScheduledThreadPoolExecutor


The ScheduledThreadPoolExecutor class inherits ThreadPoolExecutor and implements the ScheduledExecutorService interface. Mainly used to perform tasks after a given delay or perform tasks on a regular basis . The function is similar to the Timer class under the java.util package, but it is more powerful and flexible than Timer, because Timer can only control the delay or periodic execution of a single thread, while ScheduledThreadPoolExecutor corresponds to a background thread of multiple threads.

One, the creation of ScheduledThreadPoolExecutor


You can use the Executors factory class to create scheduled tasks: ScheduledThreadPoolExecutor and SingleThreadScheduledExecutor .

ScheduledThreadPoolExecutor: Applicable to scenarios where several (fixed) threads are delayed or execute tasks regularly, and the number of background threads needs to be limited in order to meet the needs of resource management.

ScheduledExecutorService stp = Executors.newScheduledThreadPool(int threadNums);
ScheduledExecutorService stp = Executors.newScheduledThreadPool(int threadNums, ThreadFactory threadFactory);

SingleThreadScheduledExecutor: It is suitable for application scenarios that require a single thread to delay or execute tasks on a regular basis, while ensuring the sequential execution of various tasks.

ScheduledExecutorService stse = Executors.newSingleThreadScheduledExecutor(int threadNums);
ScheduledExecutorService stp = Executors.newSingleThreadScheduledExecutor(int threadNums, ThreadFactory threadFactory);

Second, the realization of ScheduledThreadPoolExecutor


By viewing the source code, we can find that the realization of ScheduledThreadPoolExecutor is mainly achieved by encapsulating the task as ScheduledFutureTask . ScheduledThreadPoolExecutor adds a ScheduledFutureTask class object that implements the RunnableScheduledFutureTask interface to the blocking queue through its scheduledAtFixedTime() method or scheduledWithFixedDelay() method.

ScheduledFutureTask mainly includes 3 member variables:

private final long sequenceNumber;
private long time;
private final long period;

sequenceNumber : sequence number, used to save the order in which tasks are added to the blocking queue;
time : used to save the specific time when the task will be executed;
period : period, used to save the interval period of task execution;

In addition, the blocking queue of ScheduledTreadPoolExecutor is implemented with DelayQueue, which can realize the element delay time delayTime to get the element. In ScheduledThreadPoolExecutor, DelayQueue encapsulates a PriorityQueue to sort tasks. First, time is sorted, with the smallest time first , If the time is the same, the sequence is smaller, that is, if the time is the same, the task that was submitted first will be executed first. Because DelayQueue is an unbounded queue, the maximumPoolSize of the thread pool is invalid. The workflow of ScheduledThreadPoolExecutor is roughly as follows:


[1] The main thread calls the scheduledAtFixedRate() or scheduledWithFixedDelay() method of the ScheduledFutureTask object, wraps the Runnable or Callable object into a ScheduledFutureTask object and adds it to the blocking queue;
[2] Thread 1 in the thread pool gets the expiration from the blocking queue ScheduledFutureTask task, and execute the task; (Expired means that the current time is greater than time)
[3] After the task is executed, change the member variable time to the time of the next task to be executed, and then put the new ScheduledFutureTask task back into the block In the queue

Guess you like

Origin blog.csdn.net/zhengzhaoyang122/article/details/109347696