当前位置: 首页 > news >正文

java:CompletableFuture的简单例子

java:CompletableFuture的简单例子

package com.chz.myTest;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;@Slf4j
public class MyTestCompletableFutureTest
{public static void main1(String[] args) throws ExecutionException, InterruptedException{Future<String> completableFuture = CompletableFuture.completedFuture("Hello");String s = completableFuture.get();log.info(s);}public static void main2(String[] args) throws ExecutionException, InterruptedException{log.info("start");CompletableFuture<String> completableFuture = new CompletableFuture<>();new Thread(()->{try {Thread.sleep(2000);log.info("准备调用:complete()");completableFuture.complete("Hello");} catch (InterruptedException e) {throw new RuntimeException(e);}}).start();log.info("准备调用:completableFuture.get()");String s = completableFuture.get();     // 这里会被阻塞log.info("result: {}", s);}public static void main3(String[] args) throws ExecutionException, InterruptedException{log.info("start");CompletableFuture<String> completableFuture = new CompletableFuture<>();new Thread(()->{try {Thread.sleep(2000);log.info("准备调用:cancel()");completableFuture.cancel(true);} catch (InterruptedException e) {throw new RuntimeException(e);}}).start();log.info("准备调用:completableFuture.get()");try {String s = completableFuture.get();     // 这里会被阻塞log.info("result: {}", s);} catch (Exception e) {log.info("exception: {}, {}", e.getClass().getSimpleName(), e.getMessage(), e);}}public static void main4(String[] args) throws ExecutionException, InterruptedException{CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(()->{return "supplyAsync";});String s = supplyAsync.get();log.info("result: " + s);}public static void main5(String[] args) throws ExecutionException, InterruptedException{CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> "1");stringCompletableFuture = stringCompletableFuture.thenCompose(new Function<String, CompletionStage<String>>() {@Overridepublic CompletionStage<String> apply(String s) {log.info("s1: " + s);return CompletableFuture.supplyAsync(() -> s + " 2");}});stringCompletableFuture = stringCompletableFuture.thenCompose(new Function<String, CompletionStage<String>>() {@Overridepublic CompletionStage<String> apply(String s) {log.info("s2: " + s);return CompletableFuture.supplyAsync(() -> s + " 3");}});String s = stringCompletableFuture.get();log.info("s3: " + s);}public static void main6(String[] args) throws ExecutionException, InterruptedException{CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello").thenCombine(CompletableFuture.supplyAsync(() -> " World"),(s1, s2) -> s1 + s2);String s = completableFuture.get();log.info(s);}public static void main7(String[] args){CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "Hello").thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"),(s1, s2) -> {log.info("result: " + (s1 + s2));});}public static void main8(String[] args) throws InterruptedException{CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {log.info("1");return "Hello";});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {log.info("2");return "Beautiful";});CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {log.info("3");return "World";});CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(future1, future2, future3);voidCompletableFuture.join();}public static void main9(String[] args){CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Beautiful");CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World");String combined = Stream.of(future1, future2, future3).map(CompletableFuture::join).collect(Collectors.joining(" "));log.info(combined);}public static void main10(String[] args) throws ExecutionException, InterruptedException{CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {log.info("supplyAsync");throw new RuntimeException("Computation error!");});CompletableFuture<String> handle = completableFuture.handle((s, t) -> {log.info("handle");return (s != null) ? s : "Hello, Stranger!";});String s = handle.get();log.info("result: " + s);}}

相关文章:

java:CompletableFuture的简单例子

java&#xff1a;CompletableFuture的简单例子 package com.chz.myTest;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.uti…...

element的table获取当前表格行

需求&#xff1a;验证表格同一行的最低限价不能超过销售定价 思路&#xff1a;先获取当前行table的index&#xff0c;然后在做大小比较 1.局部html <el-table-column label"销售定价(元)" min-width"200px"><template slot"header"&…...

html做一个分组散点图图的软件

在HTML中创建一个分组散点图&#xff0c;可以结合JavaScript库如D3.js或Plotly.js来实现。这些库提供了强大的数据可视化功能&#xff0c;易于集成和使用。下面是一个使用Plotly.js创建分组散点图的示例&#xff1a; 要添加文件上传功能&#xff0c;可以让用户上传包含数据的文…...

【SQL】UNION 与 UNION ALL 的区别

在 SQL 中&#xff0c;UNION 和 UNION ALL 都用于将两个或多个结果集合并为一个结果集&#xff0c;但它们在处理重复数据方面有显著区别。以下是它们的详细区别&#xff1a; 1. UNION UNION 操作符用于合并两个或多个 SELECT 语句的结果集&#xff0c;并自动去除结果集中重复…...

分类判决界面---W-H、H-K算法

本篇文章是博主在人工智能等领域学习时&#xff0c;用于个人学习、研究或者欣赏使用&#xff0c;并基于博主对人工智能等领域的一些理解而记录的学习摘录和笔记&#xff0c;若有不当和侵权之处&#xff0c;指出后将会立即改正&#xff0c;还望谅解。文章分类在AI学习笔记&#…...

Python基础教程(三十):math模块

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; &#x1f49d;&#x1f49…...

你只是重新发现了一些东西

指北君关于另外一条思维路径的发现。 "自以为是"的顿悟时刻 有很多时候&#xff0c;我会"自以为是"的发现/发明一些东西。这种"自以为是"的时刻通常还带有一些骄傲自豪的情绪。这种感觉特别像古希腊博学家阿基米德 在苦思冥想如何测量不规则物体…...

【英伟达GPU的挑战者】Groq—AI大模型推理的革命者

目录 引言第一部分&#xff1a;Groq简介第二部分&#xff1a;Groq的特点与优势1、高性能推理加速2、近存计算技术3、专用ASIC芯片设计4、低延迟与高吞吐量5、成本效益分析6、易用性与集成性7、软件与硬件的协同设计 第三部分&#xff1a;Groq的使用指南1、准备工作2、简单使用样…...

Python学习路线

Python学习路线 领取资料 一、Python基础知识 Python入门&#xff1a;了解Python的安装方法、如何运行Python程序以及交互模式的使用&#xff0c;同时学习注释的添加方法。 数据类型&#xff1a;掌握Python中的各种数据类型&#xff0c;包括数字、布尔值、字符串、列表、元…...

C++ std::forward()

在线调试网站&#xff1a; https://wandbox.org/ #include <iostream> #include <thread> #include <mutex> void func(int &&args) {std::cout << args << std::endl; }int main () {int a 10;func(20); …...

常见的8种排序(含代码):插入排序、冒泡排序、希尔排序、快速排序、简单选择排序、归并排序、堆排序、基数排序

时间复杂度O(n^2) 1、插入排序 (Insertion Sort) 从第一个元素开始&#xff0c;该元素可以认为已经被排序&#xff1b;取出下一个元素&#xff0c;在已经排序的元素序列中从后向前扫描&#xff1b;如果该元素&#xff08;已排序&#xff09;大于新元素&#xff0c;将该元素移到…...

go语言day2

使用cmd 中的 go install &#xff1b; go build 命令出现 go cannot find main module 错误怎么解决&#xff1f; go学习-问题记录(开发环境)go: cannot find main module&#xff1b; see ‘go help modules‘_go: no flags specified (see go help mod edit)-CSDN博客 在本…...

vue echarts画多柱状图+多折线图

<!--多柱状图折线图--> <div class"echarts-box" id"multiBarPlusLine"></div>import * as echarts from echarts;mounted() {this.getMultiBarPlusLine() },getMultiBarPlusLine() {const container document.getElementById(multiBar…...

cesium for unity 打包webgl失败,提示不支持

platform webgl is not supported with HDRP use the Vulkan graphics AR instead....

python开发基础——day7 序列类型方法

一、初识序列类型方法 序列类型的概念&#xff1a;数据的集合&#xff0c;在序列类型里面可以存放任意的数据&#xff0c;也可以对数据进行更方便的操作&#xff0c;这个操作是叫增删改查(crud) ( 增加(Creat)&#xff0c;读取查询(Retrieve)&#xff0c;更新(Update)&#xf…...

用java写一个二叉树翻转

class TreeNode {int val;TreeNode left, right;TreeNode(int val) {this.val val;left right null;} }public class BinaryTree {TreeNode root;// 递归翻转二叉树public TreeNode invertTree(TreeNode root) {if (root null) {return null;}// 递归翻转左子树和右子树Tre…...

数学建模系列(3/4):典型建模方法

目录 引言 1. 回归分析 1.1 线性回归 基本概念 Matlab实现 1.2 多元回归 基本概念 Matlab实现 1.3 非线性回归 基本概念 Matlab实现 2. 时间序列分析 2.1 时间序列的基本概念 2.2 移动平均 基本概念 Matlab实现 2.3 指数平滑 基本概念 Matlab实现 2.4 ARIM…...

AI播客下载:Machine Learning Street Talk(AI机器学习)

该频道由 Tim Scarfe 博士、Yannic Kilcher 博士和 Keith Duggar 博士管理。 他们做了出色的工作&#xff0c;对每个节目进行了彻底的研究&#xff0c;并与机器学习行业中一些受过最高教育、最全面的嘉宾进行了双向对话。 每一集都会教授一些新内容&#xff0c;并且提供未经过滤…...

鱼缸补水器工作原理是什么

鱼缸补水器是一种应用广泛的智能设备&#xff0c;主要用于自动监测和补充鱼缸内的水位&#xff0c;以确保鱼类生存环境的稳定。其工作原理简单而高效&#xff0c;为饲主提供了方便和安全的使用体验。 该补水器通常由两部分组成&#xff1a;控制器和吸盘。首先&#xff0c;用户…...

Linux-Tomcat服务配置到系统服务

目录 前言一、系统环境二、配置步骤step1 了解环境的安装路径step2 配置生成tomcat.pid文件step3 配置tomcat.service文件 三、测试systemctl命令管理Tomcat服务3.1 systemctl命令启动Tomcat服务3.2 systemctl命令查看Tomcat服务3.3 systemctl命令关闭Tomcat服务3.4 systemctl命…...

【网络】每天掌握一个Linux命令 - iftop

在Linux系统中&#xff0c;iftop是网络管理的得力助手&#xff0c;能实时监控网络流量、连接情况等&#xff0c;帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

Admin.Net中的消息通信SignalR解释

定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

家政维修平台实战20:权限设计

目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系&#xff0c;主要是分成几个表&#xff0c;用户表我们是记录用户的基础信息&#xff0c;包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题&#xff0c;不同的角色&#xf…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

Java 加密常用的各种算法及其选择

在数字化时代&#xff0c;数据安全至关重要&#xff0c;Java 作为广泛应用的编程语言&#xff0c;提供了丰富的加密算法来保障数据的保密性、完整性和真实性。了解这些常用加密算法及其适用场景&#xff0c;有助于开发者在不同的业务需求中做出正确的选择。​ 一、对称加密算法…...

ElasticSearch搜索引擎之倒排索引及其底层算法

文章目录 一、搜索引擎1、什么是搜索引擎?2、搜索引擎的分类3、常用的搜索引擎4、搜索引擎的特点二、倒排索引1、简介2、为什么倒排索引不用B+树1.创建时间长,文件大。2.其次,树深,IO次数可怕。3.索引可能会失效。4.精准度差。三. 倒排索引四、算法1、Term Index的算法2、 …...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

浪潮交换机配置track检测实现高速公路收费网络主备切换NQA

浪潮交换机track配置 项目背景高速网络拓扑网络情况分析通信线路收费网络路由 收费汇聚交换机相应配置收费汇聚track配置 项目背景 在实施省内一条高速公路时遇到的需求&#xff0c;本次涉及的主要是收费汇聚交换机的配置&#xff0c;浪潮网络设备在高速项目很少&#xff0c;通…...