首页
关于
友链
统计
Search
1
wlop 4K 壁纸 4k8k 动态 壁纸
1,032 阅读
2
Nacos持久化MySQL问题-解决方案
663 阅读
3
Docker搭建Typecho博客
655 阅读
4
keytool证书导入
536 阅读
5
滑动时间窗口算法
533 阅读
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
开发工具
百度网盘资源
天翼网盘资源
阿里网盘资源
登录
Search
标签搜索
java
javase
docker
java8
springboot
thread
spring
分布式
mysql
锁
linux
redis
源码
typecho
centos
git
map
RabbitMQ
lambda
stream
少年
累计撰写
189
篇文章
累计收到
14
条评论
首页
栏目
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
开发工具
百度网盘资源
天翼网盘资源
阿里网盘资源
页面
关于
友链
统计
搜索到
1
篇与
的结果
2022-11-22
AVA8-CompletableFuture常用API:runAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf
AVA8-CompletableFuture常用API:runAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf1、runAfterBoth :2个CompletableFuture都执行完后,再执行其它操作package com.example.study.java8.completableFutures.api; import java.util.concurrent.CompletableFuture; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 1、runAfterBoth: 2个都执行完后,再执行其它操作 CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "this is runing 1......"); return 1; }) .runAfterBoth(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "this is runing 2......"); return 2; }), () -> System.out.println("done")); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(10000); } }输出结果:ForkJoinPool.commonPool-worker-5this is runing 2...... ForkJoinPool.commonPool-worker-19this is runing 1...... done2、 applyToEither:其中一个CompletableFuture执行完,就将结果传给另一个Fuctionpackage com.example.study.java8.completableFutures.api; import java.util.concurrent.CompletableFuture; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 2、runAfterBoth: 其中一个CompletableFuture执行完,就将结果传递到另一个Function中。 CompletableFuture.supplyAsync(()->{ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is one future..."); return 1; }).applyToEither(CompletableFuture.supplyAsync(()->{ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is two future..."); return 2; }), v-> 10 * v ).thenAccept(System.out::println); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(10000); } }输出结果:ForkJoinPool.commonPool-worker-5this is two future... 20 ForkJoinPool.commonPool-worker-19this is one future...3、acceptEither :其中一个future执行完,就将结果传递到cutomer中消费package com.example.study.java8.completableFutures.api; import java.util.concurrent.CompletableFuture; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 3、acceptEither: 其中一个CompletableFuture执行完,就将结果传递到另一个Function中。 CompletableFuture.supplyAsync(()->{ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is one future..."); return 1; }).acceptEither(CompletableFuture.supplyAsync(()->{ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is two future..."); return 2; }), System.out::println ); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(10000); } }输出结果:ForkJoinPool.commonPool-worker-5this is two future... 2 ForkJoinPool.commonPool-worker-19this is one future...4、runAfterEither :其中一个CompletableFuture执行完,就可以做其它操作了。不会将结果传递,可以做类似其中一个操作完后的消息通知功能。package com.example.study.java8.completableFutures.api; import java.util.concurrent.CompletableFuture; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 4、runAfterEither: 其中一个CompletableFuture执行完,就可以做其它操作了。不会将结果传递,可以做类似其中一个操作完后的消息通知功能。 CompletableFuture.supplyAsync(()->{ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is one future..."); return 1; }).runAfterEither(CompletableFuture.supplyAsync(()->{ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"this is two future..."); return 2; }),()-> System.out.println("其中一个future执行完成") ); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(10000); } }输出结果:ForkJoinPool.commonPool-worker-5this is two future... 其中一个future执行完成 ForkJoinPool.commonPool-worker-19this is one future...5、 allOf: 静态方法,可直接调用。全部future执行完后,再进行消费package com.example.study.java8.completableFutures.api; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import static java.util.stream.Collectors.toList; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 5、allOf: 静态方法,可直接调用。全部future执行完后,再进行消费 List<CompletableFuture<Double>> completableFutureList = Arrays.asList(1, 2, 3, 4, 5) .stream().map(i -> CompletableFuture.supplyAsync((CompletableFutureAction2::get))) .collect(toList()); CompletableFuture[] completableFuturesArray = completableFutureList.toArray(new CompletableFuture[completableFutureList.size()]); CompletableFuture.allOf(completableFuturesArray).thenRun(()-> System.out.println("所有future执行完成")); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(50000); } }输出结果:0.5653558041825968 0.5720868499613329 0.25643461386243427 0.9522248509043018 0.5483123698045103 所有future执行完成6、anyOf :静态方法,可直接调用。其中一个future执行完后,就进行消费package com.example.study.java8.completableFutures.api; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Random; import java.util.concurrent.CompletableFuture; import static java.util.stream.Collectors.toList; public class CompletableFutureAction2 { private final static Random RANDOM = new Random(System.currentTimeMillis()); public static void main(String[] args) throws InterruptedException { //API-- 6、anyOf: 静态方法,可直接调用。其中一个future执行完后,就进行消费 List<CompletableFuture<Double>> futureList = Arrays.asList(1, 2, 3, 4, 5) .stream() .map(i -> CompletableFuture.supplyAsync(CompletableFutureAction2::get)) .collect(toList()); CompletableFuture[] futuresArray = futureList.toArray(new CompletableFuture[futureList.size()]); CompletableFuture.anyOf(futuresArray).thenRun(()-> System.out.println("其中一个future已执行完成")); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(50000); } static double get(){ try { Thread.sleep(RANDOM.nextInt(100)); } catch (InterruptedException e) { e.printStackTrace(); } double value = RANDOM.nextDouble(); System.out.println(value); return value; } } 输出结果:0.41392777550489923 其中一个future已执行完成 0.7321854786542472 0.015636586751138104 0.8968276147964326 0.26576407363892185AVA8-CompletableFuture常用API:1、runAfterBoth2、applyToEither 3、acceptEither 4 、runAfterEither 5 、allOf 6、anyOf
2022年11月22日
151 阅读
0 评论
3 点赞