Future模式

Future模式

  • Future的核心思想是:一个耗时的方法,在调用时,立马先返回一个Future,通过Future这个数据结构去控制方法的计算过程。
  • 接口
public class FutureTask<V> implements RunnableFuture<V>

public interface RunnableFuture<V> extends Runnable, Future<V>

public interface Callable<V> {
    V call() throws Exception;
}

public interface Future<V> {
    boolean cancel(boolean mayInterruptIfRunning);// 还没计算完,可以取消计算过程
    boolean isCancelled();// 判断计算是否被取消
    boolean isDone();// 判断是否计算完
    V get() throws InterruptedException, ExecutionException;// 获取计算结果(如果还没计算完,也是必须等待的)
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
  • 场景:网购厨具 -> 超市购买食材 -> 客户做饭。
  • 同步代码:
public class CommonCook {

    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        // 1、网购厨具
        OnlineShopping thread = new OnlineShopping();
        thread.start();
        thread.join();  // 保证厨具送到
        // 2、超市购买食材
        Thread.sleep(2000);  // 模拟购买食材时间
        Foods foods = new Foods();
        System.out.println("第二步:食材到位");
        // 第三步 用厨具烹饪食材
        System.out.println("第三步:开始展现厨艺");
        cook(thread.kitchenWare, foods);
        
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }
    
    // 网购厨具线程
    static class OnlineShopping extends Thread {
        private KitchenWare kitchenWare;
        @Override
        public void run() {
            System.out.println("第一步:下单");
            System.out.println("第一步:等待送货");
            try {
                Thread.sleep(5000);  // 模拟送货时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第一步:快递送到");
            kitchenWare = new KitchenWare();
        }
    }

    // 用厨具烹饪食材
    static void cook(KitchenWare kitchenWare, Foods foods) {}
    // 厨具类
    static class KitchenWare {}
    // 食材类
    static class Foods {}
}
  • 执行结果
第一步:下单
第一步:等待送货
第一步:快递送到
第二步:食材到位
第三步:开始展现厨艺
总共用时7013ms
  • 异步代码
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureCook {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();
        // 1、网购厨具
        Callable<KitchenWare> onlineShopping = new Callable<KitchenWare>() {
            @Override
            public KitchenWare call() throws Exception {
                System.out.println("第一步:下单");
                System.out.println("第一步:等待送货");
                Thread.sleep(5000);  // 模拟送货时间
                System.out.println("第一步:快递送到");
                return new KitchenWare();
            }   
        };
        FutureTask<KitchenWare> task = new FutureTask<KitchenWare>(onlineShopping);
        new Thread(task).start();
        // 第二步 去超市购买食材
        Thread.sleep(2000);  // 模拟购买食材时间
        Foods foods = new Foods();
        System.out.println("第二步:食材到位");
        // 第三步 用厨具烹饪食材
        if (!task.isDone()) {  // 联系快递员,询问是否到货
            System.out.println("第三步:厨具还没到,心情好就等着(心情不好就调用cancel方法取消订单)");
        }
        KitchenWare kitchenWare = task.get();
        System.out.println("第三步:厨具到位,开始展现厨艺");
        cook(kitchenWare, foods);
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }
    //  用厨具烹饪食材
    static void cook(KitchenWare kitchenWare, Foods foods) {}
    // 厨具类
    static class KitchenWare {}
    // 食材类
    static class Foods {}
}
  • 执行结果
第一步:下单
第一步:等待送货
第二步:食材到位
第三步:厨具还没到,心情好就等着(心情不好就调用cancel方法取消订单)
第一步:快递送到
第三步:厨具到位,开始展现厨艺
总共用时5005ms
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值