Java FutureTask类使用案例解析

2022-10-07,,,

futuretask一个可取消的异步计算,futuretask 实现了future的基本方法,提空 start cancel 操作,可以查询计算是否已经完成,并且可以获取计算的结果。结果只可以在计算完成之后获取,get方法会阻塞当计算没有完成的时候,一旦计算已经完成,那么计算就不能再次启动或是取消。

一个futuretask 可以用来包装一个 callable 或是一个runnable对象。因为furturetask实现了runnable方法,所以一个 futuretask可以提交(submit)给一个excutor执行(excution).

futuretask是java 5引入的一个类,从名字可以看出来futuretask既是一个future,又是一个task。

我们看下futuretask的定义:

public class futuretask<v> implements runnablefuture<v> {
  ...
}
public interface runnablefuture<v> extends runnable, future<v> {
  /**
   * sets this future to the result of its computation
   * unless it has been cancelled.
   */
  void run();
}

futuretask实现了runnablefuture接口,runnablefuture接口是runnable和future的综合体。

作为一个future,futuretask可以执行异步计算,可以查看异步程序是否执行完毕,并且可以开始和取消程序,并取得程序最终的执行结果。

除此之外,futuretask还提供了一个runandreset()的方法, 该方法可以运行task并且重置future的状态。

callable和runnable的转换
我们知道callable是有返回值的,而runnable是没有返回值的。
executors提供了很多有用的方法,将runnable转换为callable:

  public static <t> callable<t> callable(runnable task, t result) {
    if (task == null)
      throw new nullpointerexception();
    return new runnableadapter<t>(task, result);
  }

futuretask内部包含一个callable,并且可以接受callable和runnable作为构造函数:

  public futuretask(callable<v> callable) {
    if (callable == null)
      throw new nullpointerexception();
    this.callable = callable;
    this.state = new;    // ensure visibility of callable
  }
  public futuretask(runnable runnable, v result) {
    this.callable = executors.callable(runnable, result);
    this.state = new;    // ensure visibility of callable
  }

它的内部就是调用了executors.callable(runnable, result);方法进行转换的。

以runnable运行

既然是一个runnable,那么futuretask就可以以线程的方式执行,我们来看一个例子:

@test
  public void convertrunnabletocallable() throws executionexception, interruptedexception {
    futuretask<integer> futuretask = new futuretask<>(new callable<integer>() {
      @override
      public integer call() throws exception {
        log.info("inside callable future task ...");
        return 0;
      }
    });

    thread thread= new thread(futuretask);
    thread.start();
    log.info(futuretask.get().tostring());
  }

上面例子是以单个线程来执行的,同样我们也可以将futuretask提交给线程池来执行:

  @test
  public void workwithexecutorservice() throws executionexception, interruptedexception {
    futuretask<integer> futuretask = new futuretask<>(new callable<integer>() {
      @override
      public integer call() throws exception {
        log.info("inside futuretask");
        return 1;
      }
    });
    executorservice executor = executors.newcachedthreadpool();
    executor.submit(futuretask);
    executor.shutdown();
    log.info(futuretask.get().tostring());
  }

本文的例子可参考https://github.com/ddean2009/learn-java-concurrency/tree/master/futuretask

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《Java FutureTask类使用案例解析.doc》

下载本文的Word格式文档,以方便收藏与打印。