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) ...
随机推荐
- [USACO2022OPEN S] COW Operations S
题目描述 Bessie 找到了一个长度不超过 \(2 \cdot 10^5\) 且仅包含字符 'C','O' 和 'W' 的字符串 \(s\).她想知道是否可以使用以下操作将该字符串变为单个字母 'C ...
- LeetCode224:基本计算器(栈)
解题思路: 1.双栈模拟,一个用来存数,一个用来存操作符.需要考虑 '('后面紧跟'+'.'-'这种情况 2.递归:遇到左括号开始递归,遇到右括号结束递归,返回值. 1 class Solution: ...
- flask上下文、g变量、current_app
在flask中的上下文分为两种 : 请求上下文 (request context) 也就是和请求相关的上下文,记录一些请求相关的数据. 包含: 1.request请求对象 2.session会话 应用 ...
- android webview(外部浏览器)调起app
最近写的项目中涉及外部浏览器以及项目webview中调起app,所以总结下,和大家分享下. 总的实现方法还是比较简单的, 1:在清单中注册 首先在AndroidManifest文件中,注册一个过滤器 ...
- C#如何对List中的Object进行排序
首先定义一个List类,这个类里面包含了Name和Total两个属性变量,下面就是针对这两个变量进行排序. public class Player { public string Name { get ...
- 云MSP服务案例|互联网商城的上云改造之旅
简介: 在中国,经过十年的发展,云计算产业已走过概念普及的1.0时期,进入"上云"和落地的2. 0阶段,企业上云意识不断增强,越来越多的企业选择部署多云和混合IT. 如今,云计算生 ...
- Kotlin委托属性(1)
在Kotlin中,委托属性(Delegated Properties)是一种强大的语言特性,允许你将属性的 getter 和 setter 方法的实现委托给其他对象.这使得你能够通过委托来重用代码.将 ...
- linux文件摘选
显示/var目录下所有以1开头,以一个小写字母结尾,且中间至少出现一位数字(可以由其他字符)的文件或目录. 命令: ls -d /var/1*[0-9]*[a-z] [root@foundation0 ...
- Docker本地部署Firefox火狐浏览器并远程访问
Docker本地部署Firefox火狐浏览器并远程访问 Firefox是一款免费开源的网页浏览器,由Mozilla基金会开发和维护.它是第一个成功挑战微软Internet Explorer浏览器垄断地 ...
- GeminiDB Cassandra接口新特性PITR发布:支持任意时间点恢复
本文分享自华为云社区<GeminiDB Cassandra接口新特性PITR发布:支持任意时间点恢复>,作者: GaussDB 数据库. 技术背景 当业务发生数据损毁.数据丢失.数据误删除 ...