前言: 最近在看RPC框架的书,书里提到了Future的使用,发现我对这个并不是很了解,所以自学一下Future。
1 Future是什么:
在并发编程中,我们经常用到非阻塞的模型,在之前的多线程的三种实现中,不管是继承thread类还是实现runnable接口,都无法保证获取到之前的执行结果。通过实现Callback接口,并用Future可以来接收多线程的执行结果。Future表示一个可能还没有完成的异步任务的结果,针对这个结果可以添加Callback以便在任务执行成功或失败后作出相应的操作。
寻找资料的过程中,发现了一个博主写的有趣的案例,看完后我觉得我有一个可以不用Future就能解决的方案,测试了,和他的时间一样,写出来大家看看,我想看完这个之后大家会更容易理解Future的
该博主的问题场景是:要做菜,得先去买厨具,买菜,这两样都齐全了之后才可以开始做菜。
一般的操作逻辑可能是:先去买菜或者先去买厨具,是串行逻辑,导致效率低下,该博主就想用Future去优化这个操作逻辑,是他们变成并行的操作。
package com.kinglong.springboot.service.impl;
/**
* 原问题:串行逻辑导致效率低下
*
* @author haojinlong
* @date 2019/7/2815:10
*/
public class CommonCook01 {
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
// 第一步 网购厨具
OnlineShopping thread = new OnlineShopping();
thread.start();
thread.join(); // 保证厨具送到
// 第二步 去超市购买食材
Thread.sleep(2000); // 模拟购买食材时间
Shicai shicai = new Shicai();
System.out.println("第二步:食材到位");
// 第三步 用厨具烹饪食材
System.out.println("第三步:开始展现厨艺");
cook(thread.chuju, shicai);
System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
}
// 网购厨具线程
static class OnlineShopping extends Thread {
private Chuju chuju;
@Override
public void run() {
System.out.println("第一步:下单");
System.out.println("第一步:等待送货");
try {
Thread.sleep(5000); // 模拟送货时间
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第一步:快递送到");
chuju = new Chuju();
}
}
// 用厨具烹饪食材
static void cook(Chuju chuju, Shicai shicai) {
}
// 厨具类
static class Chuju {
}
// 食材类
static class Shicai {
}
}
原逻辑耗时:
第一步:下单
第一步:等待送货
第一步:快递送到
第二步:食材到位
第三步:开始展现厨艺
总共用时7005ms
该博主优化后的
package com.kinglong.springboot.service.impl;
import java.util.concurrent.*;
/**
* 该博主的方案:使用FutureTask实现
*
* @author haojinlong
* @date 2019/7/2815:10
*/
public class CommonCook02 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
// 第一步 网购厨具
Callable<Chuju> onlineShopping = new Callable<Chuju>() {
@Override
public Chuju call() throws Exception {
System.out.println("第一步:下单");
System.out.println("第一步:等待送货");
Thread.sleep(5000); // 模拟送货时间
System.out.println("第一步:快递送到");
return new Chuju();
}
};
FutureTask<Chuju> task = new FutureTask<Chuju>(onlineShopping);
new Thread(task).start();
// 第二步 去超市购买食材
Thread.sleep(2000); // 模拟购买食材时间
Shicai shicai = new Shicai();
System.out.println("第二步:食材到位");
// 第三步 用厨具烹饪食材
if (!task.isDone()) { // 联系快递员,询问是否到货
System.out.println("第三步:厨具还没到,心情好就等着(心情不好就调用cancel方法取消订单)");
}
Chuju chuju = task.get();
System.out.println("第三步:厨具到位,开始展现厨艺");
cook(chuju, shicai);
System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
}
// 用厨具烹饪食材
static void cook(Chuju chuju, Shicai shicai) {}
// 厨具类
static class Chuju {}
// 食材类
static class Shicai {}
}
该博主优化后的耗时
第一步:下单
第一步:等待送货
第二步:食材到位
第三步:厨具还没到,心情好就等着(心情不好就调用cancel方法取消订单)
第一步:快递送到
第三步:厨具到位,开始展现厨艺
总共用时5105ms
以下是我的实现
package com.kinglong.springboot.service.impl;
import java.util.concurrent.*;
/**
* 我的方案:使用CountDownLatch实现
*
* @author haojinlong
* @date 2019/7/2815:10
*/
public class CommonCook03 {
private static Chuju chuju = null;
private static Shicai shicai = null;
public static void main(String[] args) throws InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(2);
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
// 第一步 网购厨具
OnlineShopping(fixedThreadPool,countDownLatch);
// 第二步 去超市购买食材
BuyShiCai(fixedThreadPool, countDownLatch);
countDownLatch.await();
cook(chuju, shicai);
System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
}
static void OnlineShopping(ExecutorService fixedThreadPool,CountDownLatch countDownLatch){
fixedThreadPool.execute(new Runnable() {
public void run() {
System.out.println("第一步:下单");
System.out.println("第一步:等待送货");
try {
Thread.sleep(5000); // 模拟送货时间
} catch (InterruptedException e) {
countDownLatch.countDown();
e.printStackTrace();
}
System.out.println("第一步:快递送到");
chuju = new Chuju();
System.out.println("第三步:厨具到位,开始展现厨艺");
countDownLatch.countDown();
}
});
}
static void BuyShiCai(ExecutorService fixedThreadPool,CountDownLatch countDownLatch){
fixedThreadPool.execute(new Runnable() {
public void run() {
try {
Thread.sleep(2000); // 模拟购买食材时间
} catch (InterruptedException e) {
countDownLatch.countDown();
e.printStackTrace();
}
shicai = new Shicai();
System.out.println("第二步:食材到位");
countDownLatch.countDown();
}
});
}
// 用厨具烹饪食材
static void cook(Chuju chuju, Shicai shicai) {}
// 厨具类
static class Chuju {}
// 食材类
static class Shicai {}
}
我的实现耗时
第一步:下单
第一步:等待送货
第二步:食材到位
第一步:快递送到
第三步:厨具到位,开始展现厨艺
总共用时5033ms
总结:
从程序的执行时间可以看出来,我的方案和答主的方案耗时其实相差不大,双方的思路一样,都是将串行变并行,不同的是它采用的是Future而我采用的是countDownLatch,因为我平时使用countDownLatch场景较多,比较熟悉。
说到这,想必大家也差不多明白了Future的使用场景,也就是用在异步执行某个请求,并且能够返回执行结果。