@

最近在学习juc并发编程,于是决定汇总一下并发编程中常用方法,常见问题以及常见考题,今天是第一章—CompletableFuture

CompletableFuture介绍

CompletableFuture是jdk8版本开始出现的类。目的是为了应用于并发编程状态下遇到的各种场景。CompletableFuture实现了CompletionStage接口和Future接口,对Java7及以前Future接口做了大量的扩展,增加了许多常用方法,增加了异步会点、流式处理、多个Future组合处理的能力,使Java在处理多任务的协同工作时更加顺畅便利。

首先得学会看懂几个函数式接口的特性!!!(重点)

1.创建异步任务

//runAsync方法不支持返回值
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
//supplyAsync可以支持返回值
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

CompletableFuture创建异步任务主要分为两个方法runAsync和supplyAsync

其中

  1. runAsync方法不支持返回值
  2. supplyAsync可以支持返回值(常用)
  3. 没有指定Executor的方法会使用ForkJoinPool.commonPool() 作为它的线程池执行异步代码。如果指定线程池,则使用指定的线程池运行。

下面是四种创建方式

public class CompletableFutureTest {
public static void main(String[] args) throws Exception{
ThreadPoolExecutor executor = new ThreadPoolExecutor(
5,5,5
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(10));
CompletableFuture future1=CompletableFuture.runAsync(()->{
System.out.println(Thread.currentThread().getName()+"*********future1 coming in");
});
//这里获取到的值是null,无返回值
System.out.println(future1.get());
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
//ForkJoinPool.commonPool-worker-9
System.out.println(Thread.currentThread().getName() + "\t" + "*********future2 coming in");
}, executor);
CompletableFuture<Integer> future3 =CompletableFuture.supplyAsync(()-> {
//pool-1-thread-1
System.out.println(Thread.currentThread().getName() + "\t" + "future3带有返回值");
return "abc";
});
System.out.println(future3.get());
CompletableFuture<Integer> future4 = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "\t" + "future4带有返回值");
return "abc";
}, executor);
System.out.println(future4.get());
//关闭线程池
executor.shutdown();
}
}

2.CompletableFuture API

①. 获得结果和触发计算(get、getNow、join、complete)

获得结果和触发计算(get、getNow、join、complete)

  1. public T get( ):不见不散(会抛出异常) 只要调用了get( )方法,不管是否计算完成都会导致阻塞
  2. public T get(long timeout, TimeUnit unit):过时不候
  3. public T getNow(T valuelfAbsent):没有计算完成的情况下,给我一个替代结果计算完,返回计算完成后的结果、没算完,返回设定的valuelfAbsent
  4. public T join( ):join方法和get( )方法作用一样,唯一区别是join方法编译时不需要手动抛异常

  1. public CompletableFuture thenApply:计算结果存在依赖关系,这两个线程串行化,由于存在依赖关系(当前步错,不走下一步),当前步骤有异常的话就叫停
  2. public CompletableFuture handle(BiFunction<? super T, Throwable, ? extends U> fn):有异常也可以往下一步走,根据带的异常参数可以进一步处理
  3. whenComplete:是执行当前任务的线程执行继续执行whenComplete的任务
  4. whenCompleteAsync:是执行把whenCompleteAsync这个任务继续提交给线程池来进行执行

②. 对计算结果进行处理(thenApply、handle)

  1. public CompletableFuture thenApply:计算结果存在依赖关系,这两个线程串行化由于存在依赖关系(当前步错,不走下一步),当前步骤有异常的话就叫停
  2. public CompletableFuture handle(BiFunction<? super T, Throwable, ? extends U> fn):有异常也可以往下一步走,根据带的异常参数可以进一步处理
  3. whenComplete:是执行当前任务的线程执行继续执行whenComplete的任务
  4. whenCompleteAsync:是执行把whenCompleteAsync这个任务继续提交给线程池来进行执行

     CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();}
return 1;
}).thenApply(s->{
System.out.println("-----1");
//如果加上int error=1/0; 由于存在依赖关系(当前步错,不走下一步),当前步骤有异常的话就叫停
//int error=1/0;
return s+1;
}).thenApply(s->{
System.out.println("-----2");
return s+2;
}).whenComplete((v,e)->{
if(e==null){
System.out.println("result-----"+v);
}
}).exceptionally(e->{
e.printStackTrace();
return null;
});
System.out.println(Thread.currentThread().getName()+"\t"+"over....");
try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {e.printStackTrace();} CompletableFuture.supplyAsync(() -> {
return 1;
}).handle((f,e) -> {
System.out.println("-----1");
return f + 2;
}).handle((f,e) -> {
System.out.println("-----2");
int error=1/0;
return f + 3;
}).handle((f,e) -> {
System.out.println("-----3");
return f + 4;
}).whenComplete((v, e) -> {
if (e == null) {
System.out.println("----result: " + v);
}
}).exceptionally(e -> {
e.printStackTrace();
return null;
}).join());

③. 对计算结果进行消费(thenRun、thenAccept、thenApply)

  1. thenRun(Runnable runnable)

    任务A执行完执行B,并且B不需要A的结果
  2. CompletableFutur thenAccept(Consumer<? super T> action)

    任务A执行完成执行B,B需要A的结果,但是任务B无返回值
  3. public CompletableFuture thenApply(Function<? super T,? extends U> fn)



4. 线程串行化方法

带了Async的方法表示的是:会重新在线程池中启动一个线程来执行任务

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor)
public CompletableFuture<Void> thenRun(Runnable action)
public CompletableFuture<Void> thenRunAsync(Runnable action)
public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor)

④. 对计算速度进行选用(applyToEither、acceptEither、runAfterEither)

  1. public CompletableFuture applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)

    这个方法表示的是,谁完成任务完成的快就返回谁的结果

  2. 两任务组合,一个完成

    applyToEither:两个任务有一个执行完成,获取它的返回值,处理任务并有新的返回值

    acceptEither:两个任务有一个执行完成,获取它的返回值,处理任务,没有新的返回值

    runAfterEither:两个任务有一个执行完成,不需要获取 future 的结果,处理任务,也没有返回值

⑤. 对计算结果进行合并(thenCombine、thenAcceptBoth、runAfterBoth)

  1. public <U,V> CompletableFuture thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)

    两个CompletionStage任务都完成后,最终把两个任务的结果一起交给thenCombine来处理先完成的先等着,等待其他分支任务

⑥. 多任务组合(allOf、anyOf)

  1. allOf:等待所有任务完成

    (public static CompletableFuture allOf(CompletableFuture<?>... cfs))
  2. anyOf:只要有一个任务完成

    (public static CompletableFuture anyOf(CompletableFuture<?>... cfs))

实战演练

案例说明:电商比价需求

同一款产品,同时搜索出同款产品在各大电商的售价;

同一款产品,同时搜索出本产品在某一个电商平台下,各个入驻门店的售价是多少

出来结果希望是同款产品的在不同地方的价格清单列表,返回一个List

in jd price is 88.05

in pdd price is 86.11

in taobao price is 90.43

(String类型)

代码示例

public class test1 {

    public ExecutorService executorService = new ThreadPoolExecutor(40,100,100,TimeUnit.MINUTES,new ArrayBlockingQueue<>(10000));
public static List<mall> list = Arrays.asList(
new mall("jd"),
new mall("tmall"),
new mall("taobao"),
new mall("pdd"),
new mall("elm")
); public static void main(String[] args) {
showRes3(list,"mysql");
}
public static List<String> showRes(List<mall> list,String productName){
long start = System.currentTimeMillis();
List<String> collect = list.stream().map(mall -> String.format(productName + " in %s price is %.2f", mall.getName(), mall.getPrice(productName))).collect(Collectors.toList());
collect.forEach(System.out::println);
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start)+"秒");
return collect;
}
public static List<String> showRes2(List<mall> list,String productName){
long start = System.currentTimeMillis();
List<String> collect = list.parallelStream().map(mall -> CompletableFuture.supplyAsync(() ->
String.format(productName + " in %s price is %.2f", mall.getName(), mall.getPrice(productName))
).join()).collect(Collectors.toList());
long end = System.currentTimeMillis();
collect.forEach(System.out::println);
System.out.println("耗时:"+(end-start)+"秒");
return collect;
} public static List<String> showRes3(List<mall> list,String productName){
long start = System.currentTimeMillis();
List<String> collect = list.stream().map(mall -> CompletableFuture.supplyAsync(() -> String.format(productName + " in %s price is %.2f", mall.getName(), mall.getPrice(productName))
)).collect(Collectors.toList()).stream().map(s -> s.join()).collect(Collectors.toList());
long end = System.currentTimeMillis();
collect.forEach(System.out::println);
System.out.println("耗时:"+(end-start)+"毫秒");
return collect;
} } class mall{
private String name; public String getName() {
return name;
} public mall(String name) {
this.name = name;
} public double getPrice(String productName){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return ThreadLocalRandom.current().nextDouble()*2+productName.charAt(0);
}
}

通过上面代码示例结果得知

通过异步线程方法耗时需要1000毫秒左右

而同步线程方法耗时需要5000毫秒

而且当商城店铺越来越多的时候,异步线程耗时不会增加,同步线程耗时会不断增加,异步的优势就显现出来了。

tips

在使用stream流的时候,如果使用的是集合的stream()方法,再使用异步线程时比如使用join()方法会出现串行,和上面方法三一样得使用两次stream流才能实现异步执行,相当于每一步都得使用stream流。

这个时候可以使用parallelStream并行流就可以解决这个问题

但是使用parallelStream会出现一些问题,所以并行流要谨慎使用!!!

并行流的陷阱

  1. 线程安全

    由于并行流使用多线程,则一切线程安全问题都应该是需要考虑的问题,如:资源竞争、死锁、事务、可见性等等。
  2. 线程消费

    在虚拟机启动时,我们指定了worker线程的数量,整个程序的生命周期都将使用这些工作线程;这必然存在任务生产和消费的问题,如果某个生产者生产了许多重量级的任务(耗时很长),那么其他任务毫无疑问将会没有工作线程可用;更可怕的事情是这些工作线程正在进行IO阻塞。

JUC并发编程(1)—CompletableFuture详解的更多相关文章

  1. 多线程JUC并发篇常见面试详解

    @ 目录 1.JUC 简介 2.线程和进程 3.并非与并行 4.线程的状态 5.wait/sleep的区别 6.Lock 锁(重点) 1.Lock锁 2.公平非公平: 3.ReentrantLock ...

  2. Java 并发编程 | 线程池详解

    原文: https://chenmingyu.top/concurrent-threadpool/ 线程池 线程池用来处理异步任务或者并发执行的任务 优点: 重复利用已创建的线程,减少创建和销毁线程造 ...

  3. 并发编程——IO模型详解

    ​ 我是一个Python技术小白,对于我而言,多任务处理一般就借助于多进程以及多线程的方式,在多任务处理中如果涉及到IO操作,则会接触到同步.异步.阻塞.非阻塞等相关概念,当然也是并发编程的基础. ​ ...

  4. Android并发编程之白话文详解Future,FutureTask和Callable

    从最简单的说起Thread和Runnable 说到并发编程,就一定是多个线程并发执行任务.那么并发编程的基础是什么呢?没错那就是Thread了.一个Thread可以执行一个Runnable类型的对象. ...

  5. 从缓存入门到并发编程三要素详解 Java中 volatile 、final 等关键字解析案例

    引入高速缓存概念 在计算机在执行程序时,以指令为单位来执行,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入. 由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这 ...

  6. 并发编程 || Java线程详解

    通用线程模型 在很多研发当中,实际应用是基于一个理论再进行优化的.所以,在了解JVM规范中的Java线程的生命周期之前,我们可以先了解通用的线程生命周期,这有助于我们后续对JVM线程生命周期的理解. ...

  7. java 并发编程lock使用详解

    浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...

  8. 跟着阿里p7一起学java高并发 - 第19天:JUC中的Executor框架详解1,全面掌握java并发核心技术

    这是java高并发系列第19篇文章. 本文主要内容 介绍Executor框架相关内容 介绍Executor 介绍ExecutorService 介绍线程池ThreadPoolExecutor及案例 介 ...

  9. java高并发系列 - 第20天:JUC中的Executor框架详解2之ExecutorCompletionService

    这是java高并发系列第20篇文章. 本文内容 ExecutorCompletionService出现的背景 介绍CompletionService接口及常用的方法 介绍ExecutorComplet ...

  10. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

随机推荐

  1. 20200411 联想Yoga 2 13升级大SSD

    20200411:下文的操作实际发生在2018年11月,当时完成了就写好了这篇文章,却忘了放上来,最近家里一个电脑的硬盘坏了,来翻箱底才找到,现在发上来. 原配置:Lonevo Yoga2 13,唯一 ...

  2. Android string.xml与Excel的互相转换

    Notice 使用以下脚本需要安装 openpyxl 和 lxml 两个库. string.xml转成Excel文件 """ 将 Android string.xml 文 ...

  3. 一文了解Go语言的函数

    1. 引言 函数是编程中不可或缺的组成部分,无论是在Go语言还是其他编程语言中,函数都扮演着重要的角色.函数能够将一系列的操作封装在一起,使得代码更加模块化.可重用和易于维护. 在本文中,我们将详细介 ...

  4. 【python基础】文件-初识文件

    文本文件可存储的数据量是非常多的.每当需要分析或修改存储在文件中的信息时,首先就是读取文件到内存中,为此可以一次性读取文件的全部内容,也可以以每次一行的方式逐步读取. 1.读取文件 1.1读取整个文件 ...

  5. 免杀系列之利用blockdlls和ACG保护恶意进程

    blockdlls Cobalt Strike 3.14版本以后添加了blockdlls功能,它将创建一个子进程并限定该子进程只能加载带有Microsoft签名的DLL. 这个功能可以阻止第三方安全软 ...

  6. React后台管理系统10 菜单数据的整理、以及其余路径的配置、刷新时默认当前选中样式

    对菜单进行数据整理 import { DesktopOutlined, FileOutlined, PieChartOutlined, TeamOutlined, UserOutlined, } fr ...

  7. 了解O2OA(翱途)开发平台中的VIP应用

    使用O2OA(翱途)开发平台可以非常方便地进行项目的业务需求开发与实施,O2OA(翱途)开发平台并不限制实现的系统类型,所以能实现的系统很多,最终呈现的项目成果也是多样性的,可能是OA系统,可能是人力 ...

  8. 4.5 x64dbg 探索钩子劫持技术

    钩子劫持技术是计算机编程中的一种技术,它们可以让开发者拦截系统函数或应用程序函数的调用,并在函数调用前或调用后执行自定义代码,钩子劫持技术通常用于病毒和恶意软件,也可以让开发者扩展或修改系统函数的功能 ...

  9. animation动画+关键帧实现轮播图效果(再次学习)!

    再次遇到要实现轮播图效果的时候,发现还是不怎么会,因为对js还没有熟练使用,只希望使用h5和css3实现效果 虽然之前已经学习了一遍了,但是还是不熟练,再次学习一下了 这次的可作为套板使用,无序列表为 ...

  10. 【Flyway】初识Flyway,将Flyway集成于Spring项目

    什么是Flyway Flyway官方网站:点击这里 官方描述: Flyway extends DevOps to your databases to accelerate software deliv ...