当前位置:网站首页>【JUC系列】Executor框架之概览
【JUC系列】Executor框架之概览
2022-06-11 23:46:00 【顧棟】
JUC中的Executor框架
文章目录
主要类图

Executor框架结构主要构成
任务:包括被执行任务,需要实现的接口:
Runnable接口或Callable接口。任务的执行:包括任务执行机制的核心接口Executor,以及继承自Executor的ExecutorService接口。Executor框架有两个关键类实现了ExecutorService接口(
ThreadPoolExecutor和ScheduledThreadPoolExecutor)。异步计算的结果:包括接口
Future和FutureTask。
主要接口
Delayed
用于标记在给定延迟后应执行的对象。此接口的实现必须定义一个compareTo方法,该方法提供与其getDelay方法一致的排序。
Future
Future 表示异步计算的结果。 提供了检查计算是否完成、等待其完成以及检索计算结果的方法。 结果只能在计算完成后使用方法 get 检索,必要时阻塞,直到它准备好。 取消是通过取消方法执行的。 提供了额外的方法来确定任务是正常完成还是被取消。 一旦计算完成,就不能取消计算。 如果您想使用 Future 来取消可取消性但不提供可用的结果,可以声明Future<?>形式的类型并返回 null 作为底层任务的结果。
ScheduledFuture
可以取消的延迟结果动作。 通常,ScheduledFuture是使用ScheduledExecutorService计划任务的结果。
RunnableScheduledFuture
可运行的 ScheduledFuture。 成功执行 run 方法会导致 Future 完成并允许访问其结果。
RunnableFuture
一个可运行的Runnable。 成功执行 run 方法会导致 Future 完成并允许访问其结果。
Runnable
它的实现类可以被ThreadPoolExecutor和ScheduledThreadPoolExecutor执行。
Executor
将任务的提交与任务执行分离开。
Callable
它的实现类可以被ThreadPoolExecutor和ScheduledThreadPoolExecutor执行。
ExecutorService
一个 Executor,它提供了管理终止的方法和可以生成 Future 以跟踪一个或多个异步任务的进度的方法。
可以关闭 ExecutorService,这将导致它拒绝新任务。提供了两种不同的方法来关闭 ExecutorService。 shutdown方法将允许先前提交的任务在终止之前执行,shutdownNow方法阻止等待的任务启动并尝试停止当前正在执行的任务。终止后,执行者没有正在执行的任务,没有等待执行的任务,也没有新的任务可以提交。应关闭未使用的ExecutorService以允许回收其资源。
方法submit通过创建和返回可用于取消执行和/或等待完成的Future扩展了基本方法Executor.execute(Runnable)。方法invokeAny和invokeAll执行最常用的批量执行形式,执行一组任务,然后等待至少一个或全部完成。 (类ExecutorCompletionService可用于编写这些方法的自定义变体。)
ScheduledExecutorService
一个 ExecutorService 可以安排命令在给定的延迟后运行,或者定期执行。
主要类
FutureTask
可取消的异步计算。 此类提供 Future 的基本实现,包括启动和取消计算、查询计算是否完成以及检索计算结果的方法。 只有在计算完成后才能检索结果; 如果计算尚未完成,get 方法将阻塞。 一旦计算完成,就不能重新开始或取消计算(除非使用 runAndReset 调用计算)。
FutureTask 可用于包装 Callable 或 Runnable 对象。 因为 FutureTask 实现了 Runnable,所以一个 FutureTask 可以提交给一个 Executor 执行。
除了作为独立类之外,此类还提供受保护的功能,在创建自定义任务类时可能很有用。
AbstractExecutorService
提供 ExecutorService 执行方法的默认实现。 该类使用 newTaskFor 返回的 RunnableFuture 实现了 submit、invokeAny 和 invokeAll 方法,默认为本包中提供的 FutureTask 类。 例如,submit(Runnable) 的实现创建了一个关联的 RunnableFuture,该 RunnableFuture 被执行并返回。 子类可以覆盖 newTaskFor 方法以返回除 FutureTask 之外的 RunnableFuture 实现。
ThreadPoolExecutor
用来执行被提交的任务
一个 ExecutorService,它使用可能的多个池线程之一执行每个提交的任务,通常使用 Executors 工厂方法进行配置。
ScheduledThreadPoolExecutor
可以在给定的延迟后运行命令,或者定期执行命令。
ScheduledFutureTask
可运行的 ScheduledFuture。 成功执行 run 方法会导致 Future 完成并允许访问其结果。
Executors
工厂类,用来创建不同的线程池。
Executors创建3种ThreadPoolExecutor和2种ScheduledThreadPoolExecutor。
CachedThreadPoolFixedThreadPoolSingleThreadExecutorScheduledThreadPoolSingleScheduleThreadPool
边栏推荐
- Ar helps brand stores achieve global data growth
- Simulated examination question bank and simulated examination of 2022 crane driver (limited to bridge crane)
- What is webstorage? And cookies
- Two way leading circular linked list (C language)
- In order to stimulate inspiration and creativity, Shanghai daoning united with XMIND to bring you full-featured mind mapping and brainstorming software
- 双向带头循环链表(C语言)
- RF中使用reuqests的两种方式
- Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
- 一文读懂Logstash原理
- 二叉排序树
猜你喜欢

Introduction and installation steps of sonarqube

Lake Shore - supertran continuous flow cryogenic thermostat system

Read 5g RF terminal industry

(interval DP | dfs+ memory) acwing 282 Stone merging

Wechat applet Bluetooth development

05 classification learning notes lihongyi's in-depth study 2021

删除收货地址【项目 商城】

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

(counting class +dp) acwing 900 Integer partition

Jetpack architecture component learning (3) -- activity results API usage
随机推荐
免费分享1个新媒体运营必备的宝藏网站
2022安全员-C证判断题模拟考试平台操作
通用树形结构的迭代与组合模式实现方案
C language leetcode deleting duplicate items in an ordered array
SAP SD 创建/修改价格表
Integrate工具之Jenkins
Simulated examination question bank and simulated examination of 2022 crane driver (limited to bridge crane)
mysql5和mysql8同时安装
Read 5g RF terminal industry
2022 R1 quick opening pressure vessel operation test questions and online simulation test
双向带头循环链表(C语言)
CD process
Introduction to Solr Basics
(dp+ group backpack) acwing 9 Group knapsack problem
mysql——find_ in_ Set usage
Acwing's first question solution attracted the first fan!!! Happy~~~
My creation anniversary
Lake Shore—SuperVariTemp 低温恒温器
Lake Shore—SuperTran-VP 连续流低温恒温器系统
Live broadcast preview | featurestore meetup V3 is coming!