当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

Pytorch: tensor operation (I) contiguous

ESP learning problem record

Basic operations of databases and tables ----- classification of data

ESP学习问题记录

MySQL時間、時區、自動填充0的問題

JS 函数提升和var变量的声明提升

Reno7 60W super flash charging architecture

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

JS function promotion and declaration promotion of VaR variable

(一)R语言入门指南——数据分析的第一步
随机推荐
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
MySQL takes up too much memory solution
Important methods of array and string
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
嵌入式启动流程
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
Kconfig Kbuild
ES6 grammar summary -- Part 2 (advanced part es6~es11)
MySQL时间、时区、自动填充0的问题
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
PT OSC deadlock analysis
A possible cause and solution of "stuck" main thread of RT thread
History object
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
STM32 how to locate the code segment that causes hard fault
arduino获取随机数
Mysqldump error1066 error solution
Générateur d'identification distribué basé sur redis
Cannot change version of project facet Dynamic Web Module to 2.3.