当前位置:网站首页>JUC forkjoin and completable future
JUC forkjoin and completable future
2022-07-06 12:22:00 【Chasing the little people on the road】
ForkJoin Code
package com.cb.demo.example;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
class MyTask extends RecursiveTask<Integer>{
private static final Integer ADJUST_VALUE=10;
private int begin;
private int end;
private int result;
public MyTask(int begin,int end){
this.begin=begin;
this.end=end;
}
@Override
protected Integer compute() {
if ((end-begin)<=ADJUST_VALUE){
for (int i = begin; i <=end ; i++) {
result=result+i;
}
}else{
int middle=(end+begin)/2;
MyTask task=new MyTask(begin,middle);
MyTask task1=new MyTask(middle+1,end);
task.fork();
task1.fork();
result=task.join()+task1.join();
}
return result;
}
}
public class ForkJoinDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyTask myTask=new MyTask(0,100);
ForkJoinPool threadPool=new ForkJoinPool();
ForkJoinTask<Integer> forkJoinTask= threadPool.submit(myTask);
System.out.println(forkJoinTask.get());
threadPool.shutdown();
}
}
CompletableFuture Code
package com.cb.demo.example;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName() + " No return ");
});
completableFuture.get();
// Asynchronous callback
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " There is a return ");
int age=10/0;
return "2022";
});
stringCompletableFuture.whenComplete((t,u)->{
System.out.println("-------t:"+t);
System.out.println("-------u:"+u);
}).exceptionally(f->{
System.out.println("***excption"+f.getMessage());
return "404";
}).get();
}
}
边栏推荐
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- Pytorch four commonly used optimizer tests
- VIM command line notes
- ESP learning problem record
- Detailed explanation of Union [C language]
- Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
- Esp8266 uses Arduino to connect Alibaba cloud Internet of things
- Classification, understanding and application of common methods of JS array
- @Autowired 和 @Resource 的区别
- 基于Redis的分布式锁 以及 超详细的改进思路
猜你喜欢

NRF24L01故障排查

Vscode basic configuration

JS function promotion and declaration promotion of VaR variable

Pytorch: tensor operation (I) contiguous

js 变量作用域和函数的学习笔记

open-mmlab labelImg mmdetection

Pat 1097 duplication on a linked list (25 points)

数据库课程设计:高校教务管理系统(含代码)

Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】

ES6 grammar summary -- Part 2 (advanced part es6~es11)
随机推荐
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
vim命令行笔记
Important methods of array and string
Cannot change version of project facet Dynamic Web Module to 2.3.
Talking about the startup of Oracle Database
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
RT thread API reference manual
[leetcode19]删除链表中倒数第n个结点
js 变量作用域和函数的学习笔记
ESP learning problem record
Arduino uno R3 register writing method (1) -- pin level state change
.elf .map .list .hex文件
Minio文件下载问题——inputstream:closed
【ESP32学习-2】esp32地址映射
A possible cause and solution of "stuck" main thread of RT thread
About using @controller in gateway
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
ESP8266连接onenet(旧版MQTT方式)
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Knowledge summary of request