当前位置:网站首页>[JUC series] overview of executor framework

[JUC series] overview of executor framework

2022-06-11 23:51:00 Gu Dong

JUC Medium Executor frame

The main class diagram

 Insert picture description here

Executor The frame structure mainly consists of

  • Mission : Including the tasks to be performed , Interfaces to be implemented :Runnable Interface or Callable Interface .

  • The execution of the mission : Including the core interface of task execution mechanism Executor, And inherited from Executor Of ExecutorService Interface .Executor The framework has two key classes implemented ExecutorService Interface (ThreadPoolExecutor and ScheduledThreadPoolExecutor).

  • The result of asynchronous calculation : Including the interface Future and FutureTask.

Main interface

Delayed

Used to mark objects that should be executed after a given delay . The implementation of this interface must define a compareTo Method , The method provides the same as getDelay Method consistent ordering .

Future

Future Represents the result of an asynchronous calculation . Provides to check whether the calculation is complete 、 Wait for it to complete and the method of retrieving the calculation results . The results can only be used after calculation get retrieval , Block if necessary , Until it's ready . Cancellation is performed through the cancellation method . Additional methods are provided to determine whether the task is completed normally or cancelled . Once the calculation is done , We can't cancel the calculation . If you want to use Future To cancel cancellability without providing available results , It can be stated that Future<?> Form and return null As a result of the underlying task .

ScheduledFuture

A delay result action that can be canceled . Usually ,ScheduledFuture It's using ScheduledExecutorService Plan the results of the task .

RunnableScheduledFuture

Operational ScheduledFuture. Successful execution run Method will result in Future Complete and allow access to its results .

RunnableFuture

A runnable Runnable. Successful execution run Method will result in Future Complete and allow access to its results .

Runnable

Its implementation class can be ThreadPoolExecutor and ScheduledThreadPoolExecutor perform .

Executor

Separate task Submission from task execution .

Callable

Its implementation class can be ThreadPoolExecutor and ScheduledThreadPoolExecutor perform .

ExecutorService

One Executor, It provides methods to manage termination and can generate Future To track the progress of one or more asynchronous tasks .

It can be turned off ExecutorService, This will cause it to reject new tasks . There are two different ways to close ExecutorService. shutdown Method will allow previously submitted tasks to execute before terminating ,shutdownNow Method prevents the waiting task from starting and attempts to stop the currently executing task . After termination , The performer has no task in progress , There are no tasks waiting to be performed , There are no new tasks to submit . Unused... Should be closed ExecutorService To allow recycling of its resources .

Method submit Creation and return can be used to cancel execution and / Or waiting to be completed Future Extended the basic method Executor.execute(Runnable). Method invokeAny and invokeAll The most commonly used form of batch execution , Perform a set of tasks , Then wait for at least one or all to be completed . ( class ExecutorCompletionService Custom variants that can be used to write these methods .)

ScheduledExecutorService

One ExecutorService You can schedule commands to run after a given delay , Or regularly .

Main categories

FutureTask

Cancelable asynchronous computation . This provides Future Basic implementation of , Including start and cancel calculation 、 Whether the query calculation is completed and how to retrieve the calculation results . The results can only be retrieved after the calculation is completed ; If the calculation is not complete ,get Method will block . Once the calculation is done , You can't restart or cancel the calculation ( Unless used runAndReset Call calculation ).

FutureTask Can be used for packaging Callable or Runnable object . because FutureTask Realized Runnable, So a FutureTask Can be submitted to a Executor perform .

In addition to being a separate class , This class also provides protected functionality , This can be useful when creating custom task classes .

AbstractExecutorService

Provide ExecutorService The default implementation of the execution method . This kind of use newTaskFor Back to RunnableFuture Realized submit、invokeAny and invokeAll Method , It defaults to the... Provided in this package FutureTask class . for example ,submit(Runnable) The implementation of creates an associated RunnableFuture, The RunnableFuture Executed and returned . Subclasses can cover newTaskFor Method to return the division FutureTask In addition to the RunnableFuture Realization .

ThreadPoolExecutor

Used to perform the submitted task

One ExecutorService, It uses one of the possible multiple pool threads to perform each submitted task , Usually use Executors Factory method for configuration .

ScheduledThreadPoolExecutor

You can run the command after a given delay , Or execute orders regularly .

ScheduledFutureTask

Operational ScheduledFuture. Successful execution run Method will result in Future Complete and allow access to its results .

Executors

Factory , Used to create different thread pools .

Executors establish 3 Kind of ThreadPoolExecutor and 2 Kind of ScheduledThreadPoolExecutor.

  • CachedThreadPool
  • FixedThreadPool
  • SingleThreadExecutor
  • ScheduledThreadPool
  • SingleScheduleThreadPool
原网站

版权声明
本文为[Gu Dong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112344356861.html