当前位置:网站首页>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();
}
}
边栏推荐
- MySQL时间、时区、自动填充0的问题
- ESP learning problem record
- Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
- Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
- Pytoch temperature prediction
- Basic knowledge of lithium battery
- GCC compilation options
- VSCode基础配置
- Basic operations of databases and tables ----- modifying data tables
- Understanding of AMBA, AHB, APB and Axi
猜你喜欢
Types de variables JS et transformations de type communes
Basic operations of databases and tables ----- classification of data
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Pytorch four commonly used optimizer tests
MySQL时间、时区、自动填充0的问题
Time slice polling scheduling of RT thread threads
arduino JSON数据信息解析
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
Vscode basic configuration
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
随机推荐
Who says that PT online schema change does not lock the table, or deadlock
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
基于Redis的分布式ID生成器
Basic operations of databases and tables ----- creating data tables
Inline detailed explanation [C language]
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
[esp32 learning-1] construction of Arduino esp32 development environment
MySQL时间、时区、自动填充0的问题
Arduino uno R3 register writing method (1) -- pin level state change
PT OSC deadlock analysis
JS regular expression basic knowledge learning
C language callback function [C language]
@The difference between Autowired and @resource
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Reno7 60W super flash charging architecture
Navigator object (determine browser type)
Esp8266 connect onenet (old mqtt mode)
Redis based distributed locks and ultra detailed improvement ideas
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
如何给Arduino项目添加音乐播放功能