当前位置: 首页 > 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命…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)

HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

k8s从入门到放弃之Ingress七层负载

k8s从入门到放弃之Ingress七层负载 在Kubernetes&#xff08;简称K8s&#xff09;中&#xff0c;Ingress是一个API对象&#xff0c;它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress&#xff0c;你可…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)

宇树机器人多姿态起立控制强化学习框架论文解析 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架&#xff08;一&#xff09; 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...

关于 WASM:1. WASM 基础原理

一、WASM 简介 1.1 WebAssembly 是什么&#xff1f; WebAssembly&#xff08;WASM&#xff09; 是一种能在现代浏览器中高效运行的二进制指令格式&#xff0c;它不是传统的编程语言&#xff0c;而是一种 低级字节码格式&#xff0c;可由高级语言&#xff08;如 C、C、Rust&am…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

智能仓储的未来:自动化、AI与数据分析如何重塑物流中心

当仓库学会“思考”&#xff0c;物流的终极形态正在诞生 想象这样的场景&#xff1a; 凌晨3点&#xff0c;某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径&#xff1b;AI视觉系统在0.1秒内扫描包裹信息&#xff1b;数字孪生平台正模拟次日峰值流量压力…...

大学生职业发展与就业创业指导教学评价

这里是引用 作为软工2203/2204班的学生&#xff0c;我们非常感谢您在《大学生职业发展与就业创业指导》课程中的悉心教导。这门课程对我们即将面临实习和就业的工科学生来说至关重要&#xff0c;而您认真负责的教学态度&#xff0c;让课程的每一部分都充满了实用价值。 尤其让我…...

使用Matplotlib创建炫酷的3D散点图:数据可视化的新维度

文章目录 基础实现代码代码解析进阶技巧1. 自定义点的大小和颜色2. 添加图例和样式美化3. 真实数据应用示例实用技巧与注意事项完整示例(带样式)应用场景在数据科学和可视化领域,三维图形能为我们提供更丰富的数据洞察。本文将手把手教你如何使用Python的Matplotlib库创建引…...