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) ...
随机推荐
- ij社区版如何创建spring项目
他们说是使用spring init什么什么的,那个都是老版的名称了,你去插件里面搜找是肯定搜不到的,现在叫spring boot helper,用这个,安装一下就好了(注意本次是在2022/11/1 ...
- 吉特日化MES & 再谈原料标签
在前面之前提到的标签的选择和设计,原料标签可以做到如下几点: 1 原料标签是一物一码还是一码多物:在美妆类的原料建议原料标签采用一物一码,对于大液洗之类的产品原料如果能够做到一物一码最佳,但是 ...
- 吉特日化MES & SQL Server 无法执行数据库脚本
打开服务器打算远程执行一下脚本,突发发现数据库无法执行脚本,就想着是不是安装了 海康AGV 控制系统的问题导致,问题如下: 问题截图如下: 报错信息如下: ====================== ...
- [ABC279G] At Most 2 Colors
Problem Statement There is a grid with $1 \times N$ squares, numbered $1,2,\dots,N$ from left to rig ...
- Nginx服务器常用参数设置
Nginx作为一个高性能的Web服务器和反向代理,它的性能可以通过调整底层操作系统的参数来进一步优化.以下是一些常见的操作系统级别的调整,通常针对Linux系统: File Descriptors L ...
- 华企盾DSC:wps个人模式无策略组新建的文件仍然加密
解决方法:右键wps安装目录手动解密即可(原因:wps模板被加密导致)
- X2加密的PcbDoc类型导出内容有误,不加密的PcbDoc导出的表格正常
出现该类问题说明读取PcbDoc文件文件出了文件,加密类型添加tmp类型即可
- Charles的奇巧淫技
大家好,我是 dom 哥.今天讨论一下 Charles 的高级用法. Charles 是 mac 电脑的一个网络代理软件,也是我平时开发常用的一个工具,用过的都说好. 本文不是 Charles 的入门 ...
- ElasticSearch之cat segments API
命令样例如下: curl -X GET "https://localhost:9200/_cat/segments?v=true&pretty" --cacert $ES_ ...
- Volcano 原理、源码分析(二)
0. 总结前置 1. 概述 2. 寻找调度器中的 PodGroup 2.1 从 PodGroup 到 JobInfo 的封装 2.2 从 Pod 到 TaskInfo 的封装 3. 控制器中 PodG ...