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) ...
随机推荐
- SpringBoot项目启动过程动态修改接口请求路径
背景 最近遇到一个技术需求,需要对其他多个已有的服务进行整合打包为一个整体的服务,项目启动过程发现一个问题,在controller层多个服务之间存在相同的RequestMapping接口请求路径,导致 ...
- Hive数据库数据表元数据导出脚本
表结构导出 ## @Title Hive库表元数据导出脚本 ## @Author changxy #!/bin/bash ##############注意修改Hive连接信息############# ...
- 使用 Kubernetes 为 CI/CD 流水线打造高效可靠的临时环境
介绍 在不断发展的科技世界中,快速构建高质量的软件至关重要.在真实环境中测试应用程序是及早发现和修复错误的关键.但是,在真实环境中设置 CI/CD 流水线进行测试可能既棘手又昂贵. Kubernete ...
- Linux笔记02: Linux环境_2.3 Linux网络连接
2.3 Linux网络连接 本节介绍VMware Workstation Player 17下CentOS 7的网络连接. 2.3.1 VMware网络类型 VMware提供的网络连接有5种: ●桥接 ...
- MyBatisPlus简介
MyBatisPlus特性 国内的一个网站 网站地址简介 | MyBatis-Plus (baomidou.com)
- 后端程序员必会的前端知识-05:React
五. React 1. React 基础 react 是前端三大框架之一 没有 vue 的基础更好,因为两者思想不太一样,不能用 vue 的习惯学习 react 需要有 js 基础,视频 19-58 ...
- MySQL中IN()按照指定列指定规则排序
现在我有这么一个需求,我需要通过IN(id1,id2,......)查询id字段,并且id字段按照IN()中的顺序排序 例如:IN(5,1,2,4) ===> 查询出来的结果也应该为 5,1,2 ...
- Oracle-lsnrctl监听进程控制
LSNRCTL> help The following operations are available An asterisk (*) denotes a modifier or extend ...
- java中final关键字的使用
1 :在java中final可以修饰类,方法,变量(包括成员变量和局部变量) 第一点:修饰类 特点:修饰的类不能被继承而且成员变量也是可以根据自己需要设置fianl 但final类 ...
- 一些JavaSE学习过程中的思路整理(三)(主观性强,持续更新中...)
目录 一些JavaSE学习过程中的思路整理(三)(主观性强,持续更新中...) Java线程同步的几种常见情况分析 由简单到复杂的几种单例模式写法 死锁的实现与破解 使用lambda表达式化简代码 J ...