Transform java future into completable future

Future is introduced in JDK 1.5 by Doug Lea to represent "the result of an asynchronous computation".

        ExecutorService exe = Executors.newCachedThreadPool();
        Runnable runnable = () -> System.out.println("Runnable");
        Callable<String> callable = () -> "Callable";
        exe.submit(runnable);
        Future<String> futureResult = exe.submit(callable);
        String result = futureResult.get();
        System.out.println(result);
        exe.shutdown();

Similar to runnable, callable can also be submitted to executor service. Differently, callable is "A task that returns a result and may throw an exception."

The call to get a future result lead current thread to wait "if necessary for the computation to complete, and then retrieves its result." Below is one implementation of get method.

    public final V get() throws InterruptedException, ExecutionException {
        int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ?
            doJoin() : externalInterruptibleAwaitDone();
        Throwable ex;
        if ((s &= DONE_MASK) == CANCELLED)
            throw new CancellationException();
        if (s == EXCEPTIONAL && (ex = getThrowableException()) != null)
            throw new ExecutionException(ex);
        return getRawResult();
    }

  

In JDK 1.8, another future, CompletableFuture, is introduced by the same author with epoch-making significance. Completable future is "A {@link Future} that may be explicitly completed (setting its value and status), and may be used as a {@link CompletionStage}, supporting dependent functions and actions that trigger upon its completion."

Now it comes to the interesting part. Hey, what if we want to convert the legacy future into completable future so as to make full use of the new features?

Well, let‘s see an snippet from stack overflow http://stackoverflow.com/questions/23301598/transform-java-future-into-a-completablefuture

    AsynchronousFileChannel open = AsynchronousFileChannel.open(Paths.get("/some/file"));
    // ...
    CompletableFuture<ByteBuffer> completableFuture = new CompletableFuture<ByteBuffer>();
    open.read(buffer, position, null, new CompletionHandler<Integer, Void>() {
        @Override
        public void completed(Integer result, Void attachment) {
            completableFuture.complete(buffer);
        }

        @Override
        public void failed(Throwable exc, Void attachment) {
            completableFuture.completeExceptionally(exc);
        }
    });
    completableFuture.thenApply(...)

If the 3rd party library support complete and fail callback, it‘s convinient to integrate with completable future. But usually, future inteferface won‘t contain these methods.

Hmmm, feels so close... and unreachable. Wait, how about ListenableFuture introduced by Guava ? It can add callback for a future.

ListeningExecutorService executor1 = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
ListenableFuture<Explosion> explosion = executor1.submit(new Callable<Explosion>() {
  public Explosion call() {
    return pushBigRedButton();
  }
});
Futures.addCallback(explosion, new FutureCallback<Explosion>() {
  // we want this handler to run immediately after we push the big red button!
  public void onSuccess(Explosion explosion) {
    walkAwayFrom(explosion);
  }
  public void onFailure(Throwable thrown) {
    battleArchNemesis(); // escaped the explosion!
  }
}, executor2);

Pattern matched, problem solved, and everyone is happy now.

Do care about executor1 and executor2, configure the thread pools properly will boost the app performance, cheers~

时间: 2024-10-12 16:00:23

Transform java future into completable future的相关文章

java.util.concuttent Callable Future详解

在传统的多线程实现方式中(继承Thread和实现Runnable)无法直接获取线程执行的返回结果,如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到效果,这样使用起来就比较麻烦. 从Java 1.5开始,java.util.concurrent包中提供了 Callable和 Future两个接口,通过它们就可以在任务执行完毕之后得到任务执行结果. Callable Callable与Runnable的功能大致相似,Callable中有一个call()函数,但是call()函数有

java多线程之Future和FutureTask

Executor框架使用Runnable 作为其基本的任务表示形式.Runnable是一种有局限性的抽象,然后可以写入日志,或者共享的数据结构,但是他不能返回一个值. 许多任务实际上都是存在延迟计算的:执行数据库查询,从网络上获取资源,或者某个复杂耗时的计算.对于这种任务,Callable是一个更好的抽象,他能返回一个值,并可能抛出一个异常.Future表示一个任务的周期,并提供了相应的方法来判断是否已经完成或者取消,以及获取任务的结果和取消任务. public interface Callab

Java多线程编程中Future模式的详解&lt;转&gt;

Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Future模式,关于其他多线程设计模式的地址如下:关于其他多线程设计模式的地址如下:关于Master-Worker模式的详解: Java多线程编程中Master-Worker模式的详解关于Guarded Suspeionsion模式的详解: Java多线程编程中Guarded Suspeionsion模式

java并发编程之future模式

1.当你想并发去执行一段代码,但是还想获取这段代码的返回结果,那么future多线程模式就可以派上用场了,代码实现如下. public class Client { public Data request() { final FutureData futureData = new FutureData(); new Thread(new Runnable() { @Override public void run() { futureData.setRealData(new RealData()

Java多线程Callable和Future类详解

     public interface Callable<V>    返回结果并且可能抛出异常的任务.实现者定义了一个不带任何参数的叫做 call 的方法      public interface Future<V>      Future 表示异步计算的结果.计算完成后只能使用 get 方法来获取结果 1.线程处理返回结果 一般开发中,使用多线程,最常见的就是:1.实现Runnable接口:2.继承Thread类. 但是run方法是没有返回结果,很难满足我们的需求.这时,常

Java 多线程中的Future

我们先来了解一下什么是Future Future模式是多线程开发中非常常见的一种设计模式.它的核心思想是异步调用.当我们需要执行一段复杂业务代码,可能要的时间很长,我们也许不是马上需要结果,那么这时候就可以把这个代码托管给Future让他慢慢处理, 我们继续做其他事情,等我什么时候需要用到的时候再来调这个结果. 可能有人会问那为什么不用Thread呢?很显然run方法是没有返回值的这时候就要用到Future. Future如何使用能够获取子线程运行的结果呢?在这里顺便提一下Callable接口,

从List[Future[T]]到Future[List[T]]

在课程<Principles Of Reactive Programming>里Week3的一节 "Promises, promises, promises"中,Erik Meijer举了一个例子,实现一个函数: def sequence[T](fs: List[Future[T]]): Future[List[T]] = {.....} 这个函数实际在Scala library的Future对象中有标准的实现. def  sequence[A, M[X] <: Tr

Java多线程 - Callable和Future

已知的创建多线程的方法有继承Tread类和实现Runnable方法.此外Java还提供了Callable接口,Callable接口也提供了一个call()方法来做为线程执行体.但是call()方法与run()方法有些不同: call()方法可以有返回值 call()方法可以抛出异常 不过Java不允许Callable对象直接作为Thread的target.而且call()方法还有一个返回值--call()方法并不是直接调用,他是做为线程执行体被调用的.Java提供了Future接口来代表Call

Java利用Callable、Future进行并行计算求和

内容:在Java中利用Callable进行带返回结果的线程计算,利用Future表示异步计算的结果,分别计算不同范围的Long求和,类似的思想还能够借鉴到需要大量计算的地方. public class Sums { public static class Sum implements Callable<Long> { private final Long from; private final Long to; public Sum(long from, long to) { this.fro