This is the 24th day of my participation in the August Text Challenge.More challenges in August

This class will rarely be used in real projects, as there are other options such as Quartz, but as a thread pool under the JUC package, we can take a look at it

ScheduledThreadPoolExecutor inherited from ThreadPoolExecutor, ScheduledThreadPoolExecutor state management, team operation, refused to operate, and is inherited in ThreadPoolExecutor; ScheduledThreadPoolExecutor mainly provides periodic tasks and delay task related operations.

It also has the following features compared to ThreadPoolExecutor:

  • Use specialized task typesScheduledFutureTaskTo perform periodic tasks, or to receive tasks that do not require time scheduling (these tasks passExecutorServiceTo execute).
  • Use a dedicated storage queueDelayedWorkQueueTo store tasks,DelayedWorkQueueIt’s an unbounded delay queueDelayQueueA. Compared with theThreadPoolExecutorIt also simplifies the enforcement mechanism.
  • Support optionalrun-after-shutdownParameter to support optional logic to decide whether to continue running the cycle or delay the task after the pool has been shut down. And when the task resubmits the operation withshutdownWhen operations overlap, the review logic is also different.

Two ScheduledThreadPoolExecutor,

In terms of the operation logic of the ScheduledThreadPoolExecutor, generally can be expressed as:

  • First of all toRunnable/CallableEncapsulated in theScheduledFutureTask, delay timetimeAs a comparison attribute.
  • Then add theDelayedWorkQueueIn the queue, take out the task with the minimum delay at the head of the queue each time, wait in timeout, and then execute it. If the execution takes the same time, the task submitted first will be executed first.ScheduledFutureTasksquenceNumberSmall variables are executed first.
  • Finally, determine whether it is a periodic task and rejoin itDelayedWorkQueueIn the queue.

1) Operation mechanism

The implementation of ScheduledThreadPoolExecutor mainly divided into two parts:

  1. When callingScheduledThreadPoolExecutorscheduleAtFixedRate()Method or **scheduleWirhFixedDelay()When the * method is used, theScheduledThreadPoolExecutorDelayQueueAdd an implementationRunnableScheduledFutureOf the interfaceScheduledFutureTask
  2. Threads in the thread pool fromDelayQueueTo deriveScheduledFutureTask“And then execute the mission.

ScheduledThreadPoolExecutor * *, the execution of the periodic task is to implement the ThreadPoolExecutor * * * * * * to do the following changes:

  • Use DelayQueue as the task queue;
  • Different parties get tasks
  • Additional processing is added after the periodic task is executed

Step 2) ScheduledThreadPoolExecutor cycle mission

  1. Thread 1 fromDelayQueueTo obtain the expiredScheduledFutureTask (DelayQueue. Take ()). A task due isScheduledFutureTaskIs greater than or equal to the current system time.
  2. Thread 1 performs thisScheduledFutureTask;
  3. Thread 1 modificationScheduledFutureTaskThe time variable of is the next time to be executed;
  4. Thread 1 changes this after timeScheduledFutureTaskPut it backDelayQueue(DelayQueue.add()).