CompletableFuture 测试类
package com.example.apidemo.completableFutrue; import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier; /**
* @Author Tim
* @Date 2021/10/25 14:18
*/
public class TestComplete { //runAsync方法不支持返回值。
//supplyAsync可以支持返回值。
public static void main1(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("run end1 ..." + Thread.currentThread().getName());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("run end2 ..." + Thread.currentThread().getName());
return "返回值:" + System.currentTimeMillis();
});
System.out.println("run end3 ...:" + future.get() + "/" + Thread.currentThread().getName());
} //当CompletableFuture的计算结果完成的回调方法
public static void main2(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("run ..." + Thread.currentThread().getName());
} catch (Exception e) {
}
System.out.println("run end ..." + Thread.currentThread().getName());
return "1234567";
}).whenComplete((String v, Throwable t) -> {
System.out.println("run end2 ..." + v + "/" + Thread.currentThread().getName() + "/" + t);
});
// v是有返回值的回调的结果,t 是线程... 可省略写法:whenComplete((v, t) -> {});
} //thenApply 方法 : 当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。
//.thenApply(Function<? super T, ? extends U> fn) T是上一个任务返回结果的类型。U:当前任务的返回值类型
public static void main3(String[] args) throws Exception {
CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {
@Override
public Long get() {
long result = new Random().nextInt(100);
System.out.println("result1 ="+result);
return result;
}
}).thenApply(new Function<Long, Long>() {
@Override
public Long apply(Long t) {
long result = t*5;
System.out.println("result2 ="+result);
return result;
}
});
long result = future.get();
System.out.println("result--------:" + result);
} //handle 是执行任务(包括出现异常)完成时对结果的处理。 而thenApply 方法,如果上个任务出现错误,则不会执行 thenApply 方法。
public static void main4(String[] args) throws Exception {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return new Random().nextInt(10);
}
}).handle(new BiFunction<Integer, Throwable, Integer>() {
@Override
public Integer apply(Integer param, Throwable throwable) {
int result = 1;
if (throwable == null){
result = param * 2;
System.out.println("apply: ==== " + param);
} else {
System.out.println("error: ==== " + throwable.getMessage());
}
return result;
}
});
System.out.println("result--------:" + future.get());
} //thenAccept 接收任务的处理结果,并消费处理,无返回结果。没有后续的输出操作, 所以future.get() 是null
public static void main5(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return new Random().nextInt(10);
}
}).thenAccept(integer -> {
System.out.println("integer====" + integer);
});
System.out.println("result--------:" + future.get());
} // thenRun: 该方法同 thenAccept 方法类似。不同的是上个任务处理完成后,并不会把计算的结果传给 thenRun 方法。
// 只是处理玩任务后,执行 thenAccept 的后续操作
public static void main6(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return new Random().nextInt(10);
}
}).thenRun(() -> {
System.out.println("thenRun ..." + 1);
});
System.out.println("result--------:" + future.get());
} //thenCombine 合并任务
public static void main(String[] args) throws Exception {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "zhangshan";
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "lisi";
}
});
CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() {
@Override
public String apply(String t, String u) {
return t + "===" + u;
}
});
System.out.println("result--------:" + result.get());
} }
CompletableFuture 测试类的更多相关文章
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。
22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- TestNG之执行测试类方式
TestNG提供了很多执行方式,下面做简单介绍. 1.XML指明测试类,按照类名执行,其中可以指定包名,也可指定无包名: 带包名,运行ParameterSample类和ParameterTest类 & ...
- XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)
当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...
- 各种数据库连接代码的测试类(java)
测试类: public class Mytest { Connection conn=null; Statement stmt=null; String myDriver="com.mysq ...
- 编写测试类,了解ArrayList的方法
这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...
- maven编译的时候排除junit测试类
maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...
- Android中的测试类配置AndroidManifest.xml
测试类至于要把一个类继承ActivityTestCase即可至于方法,根据需要自己建立方法:之后必须配置AnroidMainfest.xml文件 配置AndroidManifest.xml文件 1) ...
随机推荐
- 现代统计分析软件Datainside在学生成绩分析中的应用
Datainside(薪火数据)是一款非常常用的统计分析软件,广泛应用于学术研究和商业领域. 在学生成绩分析方面,Datainside提供了丰富的功能和工具,可以帮助教育机构和研究人员深入理解学生的学 ...
- 不要用第三方日志包了Microsoft.Extensions.Logging功能就很强大
在.NET中,Microsoft.Extensions.Logging是一个广泛使用的日志库,用于记录应用程序的日志信息.它提供了丰富的功能和灵活性,使开发人员能够轻松地记录各种类型的日志,并将其输出 ...
- [ABC248G] GCD cost on the tree
Problem Statement You are given an undirected tree with $N$ vertices. Let us call the vertices Verte ...
- Python 提取 Word 文档中的文本和图片
将内容从 Word 文档中提取出来可以方便我们对其进行其他操作,如将内容储存在数据库中.将内容导入到其他程序中.用于 AI 训练以及制作其他文档等.第三方库 Spire.Doc for Python ...
- C++ Qt开发:MdiArea多窗体组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍MdiArea ...
- 文心一言 VS 讯飞星火 VS chatgpt (31)-- 算法导论5.2 3题
三.利用指示器随机变量来计算掷n 个骰子之和的期望值. 文心一言: 为了计算掷n个骰子之和的期望值,我们需要先了解一个重要的概念:指示器随机变量. 指示器随机变量是一种特殊的随机变量,它只有两个取值: ...
- 深度探秘.NET 5
今年11月10号 .NET 5.0 如约而至.这是.NET All in one后的第一个版本,虽然不是LTS(Long term support)版本,但是是生产环境可用的. 有微软的背书,微软从. ...
- Java 给PPT中的表格设置分布行和分布列
在表格中可设置"分布行"或"分布列"将行高.列宽调整为协调统一的高度或宽度,是一种快速实现表格排版的方法之一.下面,通过Java后端程序代码介绍如何在PPT幻灯 ...
- Java PDF文档转换 — PDF转Excel、SVG转PDF
概述 Spire.PDF for Java支持将PDF文档高质量地转换为XPS.图片.SVG.Word.HTML和PDF/A格式,以及支持将XPS.HTML文档转换为PDF格式.本文将通过代码演示来介 ...
- C++篇:第十一章_标准库_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 十一.标准库 include头文件: ① 一般来说,导入objective c的 ...