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 测试类的更多相关文章

  1. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

  2. 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...

  3. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

  4. 在Eclipse中生成接口的JUnit测试类

    在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...

  5. TestNG之执行测试类方式

    TestNG提供了很多执行方式,下面做简单介绍. 1.XML指明测试类,按照类名执行,其中可以指定包名,也可指定无包名: 带包名,运行ParameterSample类和ParameterTest类 & ...

  6. XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)

    当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...

  7. 各种数据库连接代码的测试类(java)

    测试类: public class Mytest { Connection conn=null; Statement stmt=null; String myDriver="com.mysq ...

  8. 编写测试类,了解ArrayList的方法

    这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...

  9. maven编译的时候排除junit测试类

    maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...

  10. Android中的测试类配置AndroidManifest.xml

    测试类至于要把一个类继承ActivityTestCase即可至于方法,根据需要自己建立方法:之后必须配置AnroidMainfest.xml文件 配置AndroidManifest.xml文件 1) ...

随机推荐

  1. 黑客玩具入门——4、漏洞扫描与Metasploit

    1.Legion漏洞扫描分析工具 Legion是Sparta的一个分支,它是一个开源的.易于使用的.超级可扩展的.半自动的网络渗透测试框架.它是一款Kali Linux系统默认集成的Python GU ...

  2. 记一次逆向分析解密还原Class文件

    前言 前阵子我的一位朋友发来一份代码让我帮忙看看.具体就是所有的jsp文件内容和大小都一样,漏洞挖掘无从下手.经过分析发现所有的Class都使用了自定义的加密工具加密,经过逆向分析,顺利解密,因而有了 ...

  3. 银河麒麟V10(飞腾ARM CPU)安装KVM踩坑记

    服务器配置信息 品牌:GreetWall CPU:飞腾FT-2000+/64 64bit 操作系统:Linux-4.19.90-24.4.v2101.ky10.aarch64-with-kylin-1 ...

  4. [CF1844G] Tree Weights

    题目描述 You are given a tree with $ n $ nodes labelled $ 1,2,\dots,n $ . The $ i $ -th edge connects no ...

  5. 27、Type关键字

    1.是什么? type是go语法里额重要而且常用的关键字,type绝不只是对应于C/C++中的typeof.搞清楚type的使用,就容易理解Go语言中的核心概念struct.interface.函数等 ...

  6. 试试这 6 个小技巧,提升 EF Core 性能

    Entity FrameWork(简称 EF)以面向对象的方式操作数据库给开发人员带来了很大的便利性,但其性能问题从面世以来就一直就被广大的 .NET 生态开发技术人员所吐槽,然而,它真的那么不堪使用 ...

  7. python脚本抢大麦网演唱会门票 ---保姆级教程 python脚本抢大麦网演唱会门票

    python脚本抢大麦网演唱会门票 流程: 1.下载并安装anaconda:https://repo.continuum.io/archive/ 下载对应linux/mac/windows版本 2.下 ...

  8. 记一次 .NET某收银软件 非托管泄露分析

    一:背景 1. 讲故事 在我的分析之旅中,遇到过很多程序的故障和杀毒软件扯上了关系,有杀毒软件导致的程序卡死,有杀毒软件导致的程序崩溃,这一篇又出现了一个杀毒软件导致的程序非托管内存泄露,真的是分析多 ...

  9. Angular 实现分页器组件

    很感谢 angular实现简单的pagination分页组件 - Amor丶Diamond - 博客园 (cnblogs.com) , 我根据这位博主代码做了修改, 增加了跳转和每页行数功能. 先看图 ...

  10. 从标准到开发,解读基于MOF的应用模型管理

    摘要:为了打破技术与业务的壁垒,搭建技术与业务的桥梁,因此基于如下流程实现应用业务模型管理 ROMA ABM. 在数字经济时代,数据正在成为企业极其重要的战略性资产.在政府方面,数据第一次作为新型生产 ...