当前位置:网站首页>[JUC series] overview of executor framework
[JUC series] overview of executor framework
2022-06-11 23:51:00 【Gu Dong】
JUC Medium Executor frame
List of articles
The main class diagram

Executor The frame structure mainly consists of
Mission : Including the tasks to be performed , Interfaces to be implemented :
RunnableInterface orCallableInterface .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 (
ThreadPoolExecutorandScheduledThreadPoolExecutor).The result of asynchronous calculation : Including the interface
FutureandFutureTask.
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.
CachedThreadPoolFixedThreadPoolSingleThreadExecutorScheduledThreadPoolSingleScheduleThreadPool
边栏推荐
猜你喜欢

VS code 编写汇编代码【微机原理】

HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications

Introduction to Solr Basics

C language leetcode deleting duplicate items in an ordered array

通用树形结构的迭代与组合模式实现方案

Simulated examination question bank and simulated examination of 2022 crane driver (limited to bridge crane)

(linear DP | monotone queue) acwing 895 Longest ascending subsequence
![[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis](/img/35/a23646ac8501ac646cd44eeecd5b79.jpg)
[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis

How many steps does it take for C language to become Fibonacci number

帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
随机推荐
Detailed explanation of various objects in browser object
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
我的创作纪念日
2022 operation of simulation examination platform for safety officer C certificate
What is bom? And knowledge points
栈(C语言)
VS code 编写汇编代码【微机原理】
Lake Shore - supertran VP continuous flow cryogenic thermostat system
DOM知识点总结
Lake Shore—SuperTran-VP 连续流低温恒温器系统
Single page pull-down refresh and double page pull-down refresh of MUI
Read 5g RF terminal industry
Lake Shore—SuperTran 连续流低温恒温器系统
Implementation scheme of iteration and combination pattern for general tree structure
What is webstorage? And cookies
C collection of questions for project review
通用树形结构的迭代与组合模式实现方案
mysql5和mysql8同时安装
2022 high voltage electrician test question simulation test question bank and online simulation test
One to one correspondence of multiple schematic diagrams and PCB diagrams under Altium designer project