当前位置:网站首页>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();
}
}
边栏推荐
- Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
- JS数组常用方法的分类、理解和运用
- Bubble sort [C language]
- 基於Redis的分布式ID生成器
- JS正则表达式基础知识学习
- JS variable types and common type conversions
- JS变量类型以及常用类型转换
- vim命令行笔记
- Basic operations of databases and tables ----- classification of data
- ORA-02030: can only select from fixed tables/views
猜你喜欢
ES6 grammar summary -- Part I (basic)
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
JS正则表达式基础知识学习
Types de variables JS et transformations de type communes
MySQL占用内存过大解决方案
JS 函数提升和var变量的声明提升
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Single chip Bluetooth wireless burning
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Learning notes of JS variable scope and function
随机推荐
单片机蓝牙无线烧录
E-commerce data analysis -- salary prediction (linear regression)
A possible cause and solution of "stuck" main thread of RT thread
(五)R语言入门生物信息学——ORF和序列分析
ES6 grammar summary -- Part I (basic)
Pytoch implements simple linear regression demo
Dead loop in FreeRTOS task function
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
ESP learning problem record
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Pytorch: tensor operation (I) contiguous
Symbolic representation of functions in deep learning papers
Priority inversion and deadlock
Cannot change version of project facet Dynamic Web Module to 2.3.
JS variable types and common type conversions
. elf . map . list . Hex file
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
[899]有序队列
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
MySQL replacement field part content