美文网首页
Future模式

Future模式

作者: jiahzhon | 来源:发表于2020-10-06 02:22 被阅读0次
  • 调用类
import java.util.function.Consumer;

/**
 * Future        ->代表的是未来的一个凭据
 * FutureTask    ->将你的调用逻辑进行了隔离
 * FutureService ->桥接 Future和 FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();

        //在此设计模式里FutureTask里编写的就是需要执行的任务
        FutureTask<String> futureTask = new FutureTask<String>() {
            @Override
            public String call() {
                try {
                    Thread.sleep(10000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "Hey,finish!!!!!!!";
            }
        };
        //类比订蛋糕后,不是等一段时间我去打电话问蛋糕好没好,而是我留下联系方式,做好了让店家打电话给我
        //此为优化,Consumer是java1.8的新接口
        //如果没有做优化,在下面还要主动调用
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

         //如果没有做优化,在下面获取完submit返回的Future对象,还要调用Future的get方法
         futureService.submit(futureTask, consumer);

        System.out.println("===========");
        System.out.println(" do other thing.");
        Thread.sleep(1000);
        System.out.println("===========");
    }

}
  • Future
public interface Future<T> {

    T get() throws InterruptedException;

}
  • Future实现类
public class AsynFuture<T> implements Future<T> {

    private volatile boolean done = false;

    private T result;

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

    @Override
    public T get() throws InterruptedException {
        synchronized (this) {
            while (!done) {
                this.wait();
            }
        }
        return result;
    }
}
  • FutureTask
public interface FutureTask<T> {

    T call();
}
  • 连接任务和未来的Service(其实运行到这里是有两条线程)
public class FutureService {
    
    //未优化
    public <T> Future<T> submit(final FutureTask<T> task) {
        AsynFuture<T> asynFuture = new AsynFuture<>();
        new Thread(() -> {
            T result = task.call();
            asynFuture.done(result);
        }).start();
        return asynFuture;
    }
    
    //已优化
    public <T> Future<T> submit(final FutureTask<T> task, final Consumer<T> consumer) {
        AsynFuture<T> asynFuture = new AsynFuture<>();
        new Thread(() -> {
            T result = task.call();
            asynFuture.done(result);
            consumer.accept(result);
        }).start();
        return asynFuture;
    }
}

相关文章

  • Java8新的异步编程方式 CompletableFuture(

    一. Future JDK 5引入了Future模式。Future接口是Java多线程Future模式的实现,在j...

  • Java8 CompletableFuture指北

    一、JAVA 异步处理的演变 1.1 Future JDK 5引入了Future模式。Future模式是多线程设计...

  • Java并发编程——CompletableFuture详解

    一、简介 JDK 5引入了Future模式。Future接口是Java多线程Future模式的实现,在java.u...

  • Java中的Future模式

    Future模式 核心思想是异步调用,在java中内置了对Future模式的实现,主要就是Future接口、Cal...

  • CompletableFuture打开方式

    前言: Future模式是CompletableFuture的基础,Future模式有哪些应用场景呢? 超时控制场...

  • 多线程Future设计模式的两种用法

    多线程Future设计模式 Future接口定义: Future接口的实现类: 封装任务的FutureTask接口...

  • Future

    Future模式 概念 ```Future模式是多线程设计常用的一种设计模式,类似商品订单。商品下单后,会立即得到...

  • java初入多线程18

    Future 模式 该模式核心思想是异步调用。 jdk 中的Future 模式 在这里面我们首先创建FutureT...

  • Future模式

    https://www.cnblogs.com/lcngu/p/5289605.html Future模式简介 F...

  • Future模式

    一、定义Future模式用来获取线程的执行结果。在Thread-Per-Message模式中,如果调用一个线程异步...

网友评论

      本文标题:Future模式

      本文链接:https://www.haomeiwen.com/subject/okbzuktx.html