多线程下的设计模式-future设计模式

在开发中常常会出现一种情况,一个任务需要执行很长的时间,但是你肯定不希望它会阻塞你的线程,在java中有一个Future的类,它相当于一个凭证,当你需要执行结果的时候给你,这个凭证是立即返回的,当你需要的时候去拿结果就行了。

这里写图片描述

Future与Future的简单实现

public interface Future<T> {

    T get() throws InterruptedException;
}


public class MyFuture<T> implements Future<T> {

    private T result;

    private volatile boolean isDone = false;

    public void done(T result) {
        synchronized (this){
            this.result = result;
            isDone = true;
            notifyAll();
        }
    }

    @Override
    public T get() throws InterruptedException {
        while (!isDone) {
            synchronized (this) {
                wait();
            }
        }
        return result;
    }
public interface Task<T> {
    T call();
}
public class FutureService<T> {

    public Future<T> submit(Task<T> task){
        MyFuture<T> future=new MyFuture<>();
        Executors.newSingleThreadExecutor().submit(()->{
            T call = task.call();
            future.done(call);
        });
        return future;
    }
}

测试执行

public class Test {
    public static void main(String[] args) {
        FutureService<String> futureService =new FutureService<>();
        Future<String> future = futureService.submit(() -> {
            try {
                //模拟处理很重的任务
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "finish";
        });
        System.out.println("doSomething");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("will to get");
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你会发现,不会阻塞主线程,当你提交这个任务后可以做自己的事情,当你需要的时候再去拿。
但是发现另外一个问题,我自己doSomthing的时间可能只有一秒,但是你处理需要10秒,那么我去get()的时候依旧会被阻塞。这样的情况是我们并不想看到的,那么怎么去解决这个问题?

我们在FutureService中添加一个方法

 public Future<T> submit(Task<T> task, Consumer<T> consumer){
        MyFuture<T> future=new MyFuture<>();
        Executors.newSingleThreadExecutor().submit(()->{
            T result = task.call();
            future.done(result);
            consumer.accept(result);
        });
        return future;
    }

你传给我一个Conusmer,是一个回调函数,你告诉我,当结果执行完毕以后你想要去干什么,我回主动帮你去回调这个处理,那么你就可以完全不需要调用get()去获取结果,并进行处理,你只需要提起告诉我你想怎么去处理这个结果,我帮你处理就可以了,在ConmletableFutre,Netty等中大量用到了这种模式,可以非常高效的处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值