当前位置:网站首页>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 .
边栏推荐
- DHCP的概念和原理
- Esxi virtual machine starts, and the module "monitorloop" fails to power on
- QGIS series (1) -qgis (server APACHE) win10 installation
- Relevant preparation materials for system design
- 客户案例 | 聚焦流程体验,助银行企业APP迭代
- Shell -- conditional statements (if statements, case statements)
- Record of pychart running jupyter notebook in virtual environment
- Linux安装Redis操作
- Brief introduction of chip, memory and its key indicators I
- Problems related to compilation and training of Darknet yolov3 and Yolo fast using CUDA environment of rtx30 Series graphics card on win10 platform
猜你喜欢

RAID详解与配置

Sunflower teaches you how to prevent denial of service attacks?

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

改善宝宝过敏就吃伊敏舒,azg与Aibeca爱楽倍佳携手守护中国宝宝成长

客户案例 | 聚焦流程体验,助银行企业APP迭代

PSI | CSI and ROC | AUC and KS - memorandum

Use of getattr, hasattr, delattr and setattr in reflectors

磁盘管理与文件系统

PSI|CSI和ROC|AUC和KS -备忘录

多模态数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台
随机推荐
PSI | CSI and ROC | AUC and KS - memorandum
Soul submitted an application for listing in Hong Kong stocks, accelerating the diversified and scene based layout of social gathering places
Soul 递交港股上市申请,加快社交聚集地多元化、场景化的布局
Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!
Shell script one click configuration lamp
Shell -- custom variables and assignments
Shell Function
What if the website server is attacked? Sunflower tips that preventing loopholes is the key
Some applications of std:: bind and std:: function
darknet-yolov3、yolo-fastect使用rtx30系显卡cuda环境在win10平台编译与训练的相关问题
pycharm在虚拟环境下跑jupyter notebook问题记录
Linux安装与卸载MySql
Keras OCR instance test
Shell sentence judgment exercise
Publish a building segmentation database with a resolution of 0.22m
Add virtual network card and configure OP route in win10
Account management and authority
EasyCVR设备管理列表页面搜索时,分页数据不显示的问题修复
Use -wall to clear code hidden dangers
win10 添加虚拟网卡,配置op路由