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

python/java环境配置

环境变量放一起 python&#xff1a; 1.首先下载Python Python下载地址&#xff1a;Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个&#xff0c;然后自定义&#xff0c;全选 可以把前4个选上 3.环境配置 1&#xff09;搜高级系统设置 2…...

ETLCloud可能遇到的问题有哪些?常见坑位解析

数据集成平台ETLCloud&#xff0c;主要用于支持数据的抽取&#xff08;Extract&#xff09;、转换&#xff08;Transform&#xff09;和加载&#xff08;Load&#xff09;过程。提供了一个简洁直观的界面&#xff0c;以便用户可以在不同的数据源之间轻松地进行数据迁移和转换。…...

OpenLayers 分屏对比(地图联动)

注&#xff1a;当前使用的是 ol 5.3.0 版本&#xff0c;天地图使用的key请到天地图官网申请&#xff0c;并替换为自己的key 地图分屏对比在WebGIS开发中是很常见的功能&#xff0c;和卷帘图层不一样的是&#xff0c;分屏对比是在各个地图中添加相同或者不同的图层进行对比查看。…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

回溯算法学习

一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

JavaScript基础-API 和 Web API

在学习JavaScript的过程中&#xff0c;理解API&#xff08;应用程序接口&#xff09;和Web API的概念及其应用是非常重要的。这些工具极大地扩展了JavaScript的功能&#xff0c;使得开发者能够创建出功能丰富、交互性强的Web应用程序。本文将深入探讨JavaScript中的API与Web AP…...

力扣热题100 k个一组反转链表题解

题目: 代码: func reverseKGroup(head *ListNode, k int) *ListNode {cur : headfor i : 0; i < k; i {if cur nil {return head}cur cur.Next}newHead : reverse(head, cur)head.Next reverseKGroup(cur, k)return newHead }func reverse(start, end *ListNode) *ListN…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

逻辑回归暴力训练预测金融欺诈

简述 「使用逻辑回归暴力预测金融欺诈&#xff0c;并不断增加特征维度持续测试」的做法&#xff0c;体现了一种逐步建模与迭代验证的实验思路&#xff0c;在金融欺诈检测中非常有价值&#xff0c;本文作为一篇回顾性记录了早年间公司给某行做反欺诈预测用到的技术和思路。百度…...

MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)

macos brew国内镜像加速方法 brew install 加速formula.jws.json下载慢加速 &#x1f37a; 最新版brew安装慢到怀疑人生&#xff1f;别怕&#xff0c;教你轻松起飞&#xff01; 最近Homebrew更新至最新版&#xff0c;每次执行 brew 命令时都会自动从官方地址 https://formulae.…...