当前位置:网站首页>Future, futuretask and completable future are often asked in interviews
Future, futuretask and completable future are often asked in interviews
2022-07-27 06:52:00 【Minor Wang Zhi】
Catalog
1.Callable and Runnable The difference between
- Callable and Runnable Are abstractions of tasks , Are abstractions of logical content executed by threads .
- Callable Support return value ,Runnable Return value... Is not supported .
- Callable Support throwing checked exceptions ,Runnable I won't support it .
- Callable combination Future It can realize the complex functions of thread asynchronous tasks .
2.Future Interface
Future Interface is the cancellation of asynchronous tasks 、 State of the query 、 Get the abstract definition of the result . His abstract methods are :
- boolean cancel(boolean mayInterruptIfRunning): Cancel the execution of the task , The parameter controls whether to interrupt the task immediately or wait until the task is completed .
- boolean isCancelled(): Determine if the task has been canceled .
- boolean isDone(): Judge whether the task has been completed .
- V get(): Wait for the task to finish , Get the return value , If the mission is cancelled , Exceptions will be thrown during waiting .
- V get(long timeout, TimeUnit unit): Timeout waiting for the task to end , If there is no result after timeout, it will throw Timeout abnormal .
3.FutureTask class
FutureTask Realized RunnableFuture Interface , Indirectly realized Future、Runnable.FutureTask Can be regarded as Runnable To perform the task , You can also think of it as Callable To pass Future Get task results .
FutureTask task = new FutureTask<>(job); // job The class of the instance should implement Runnable perhaps Callable.
It should be noted that ,FutureTask The task state of is one-way , Once the task is completed , His state is irreversible .
FutureTask There are also shortcomings :
- Cannot combine multiple asynchronous tasks .
- The exception handling mechanism is not enough .
- Cannot serialize multiple asynchronous tasks .
4.CompletableFuture
CompletableFuture It is a powerful for multiple asynchronous task choreography JUC Tools under package ,CompletableFuture It's right Future Expansion and enhancement of .CompletableFuture When submitting a task, you can specify the incoming thread pool , If it doesn't come in , It is used by default JDK Self contained ForkJoinPool.commonPool() Thread pool . Its common method :
- thenApply(): Hand over the execution results of the previous tasks to the following Function.
- thenCombine: Consolidation of tasks , return .
- runAfterBoth: After even a task is completed , Then proceed to the next task .
- anyOf()、allOf(): Orchestrate multiple asynchronous tasks , Wait for individual completion or Multiple completion .
What I use most in the project is probably the splitting and summarizing of multiple tasks , Improve the response speed of the interface based on the overall scenario :
CompletableFuture There are 2 An asynchronous task execution method :runAsync、supplyAsync The difference between them is that the latter supports scenarios with return values .
CompletableFuture Provided join and get Method , All to get CompletableFuture Result . The difference is ,join Method throws an unchecked exception ,get What is thrown is the detected exception, which needs to be handled manually .
边栏推荐
- 磁盘管理与文件系统
- numpy数组和图片互转
- NFS简介和配置
- px4源码编译之 建立自己的程序模块
- To improve the baby's allergy, take yiminshu. Azg and aibeca love la Beijia work together to protect the growth of Chinese babies
- NFS introduction and configuration
- About the problem that Druid can't connect to the database
- 如何让最小 API 绑定查询字符串中的数组
- Li Hongyi 2020 deep learning and human language processing dlhlp conditional generation by RNN and attention-p22
- Use -wall to clear code hidden dangers
猜你喜欢

MySql数据库

向日葵全面科普,为你的远程控制设备及时规避漏洞

Boostrap

Shell -- operation of variables

RAID详解与配置

DNS domain name resolution service

Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!
![Summary of frequently asked questions in the interview [summarized after painstaking work all night]](/img/70/90543c23326305590242716b989643.png)
Summary of frequently asked questions in the interview [summarized after painstaking work all night]

FTX US推出FTX Stocks,向主流金融行业迈进

Project training experience 2
随机推荐
Solve the problems of CONDA install stop and interruption
About the problem that Druid can't connect to the database
ES6新特性(入门)
What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
如何让最小 API 绑定查询字符串中的数组
Shell sentence judgment exercise
备忘录 @RestControllerAdvice与异常拦截类示例
Summary of frequently asked questions in the interview [summarized after painstaking work all night]
After adding a camera (camera) to the UAV in gazebo, the UAV cannot take off
程序、进程、线程、协程以及单线程、多线程基本概念
keras-ocr实例测试
智能安防视频平台EasyCVR出现通道列表为空情况的原因是什么?
Is it feasible to fix the vulnerability with one click? Sunflower to tell you that one click fix vulnerability is feasible? Sunflower to tell you that one click fix vulnerability is feasible? Sunflowe
Redis operation of Linux Installation
众多世界500强企业集聚第二届数博会,数字产业大幕即将开启!
关于过快S验证码的一些问题
1. Install redis in CentOS 7
Sunflower: don't worry when you encounter computer vulnerabilities, understand clearly and then judge sunflower: don't worry when you encounter computer vulnerabilities, understand clearly and then ju
Linux Installation and uninstallation of MySQL
ESXI虚拟机启动,模块“MonitorLoop”打开电源失败