当前位置:网站首页>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 takes up too much memory solution
- [esp32 learning-2] esp32 address mapping
- Understanding of AMBA, AHB, APB and Axi
- ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
- level16
- Use of lists
- Redis based distributed locks and ultra detailed improvement ideas
- @The difference between Autowired and @resource
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
猜你喜欢
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Oppo vooc fast charging circuit and protocol
Types de variables JS et transformations de type communes
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
ES6 grammar summary -- Part I (basic)
JS function promotion and declaration promotion of VaR variable
RT thread API reference manual
Analysis of charging architecture of glory magic 3pro
【ESP32学习-1】Arduino ESP32开发环境搭建
MySQL takes up too much memory solution
随机推荐
Générateur d'identification distribué basé sur redis
Basic operations of databases and tables ----- classification of data
Imgcat usage experience
arduino获取数组的长度
[leetcode19]删除链表中倒数第n个结点
[leetcode622]设计循环队列
Inline detailed explanation [C language]
Important methods of array and string
JS变量类型以及常用类型转换
ES6 grammar summary -- Part I (basic)
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
【ESP32学习-1】Arduino ESP32开发环境搭建
Use of lists
Bubble sort [C language]
vim命令行笔记
Kaggle competition two Sigma connect: rental listing inquiries
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
VIM command line notes