022 Future接口

一 . 概述

上一节中我们提到了Future对象,是从Callable接口中封装返回接口的对象.

  这一节就说一下Future接口.


二 .Future接口 

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; }

  这个接口总的来说就是描述了返回结果的对象,这是一个异步的方式.只有调用了get()方法的时候会发生阻塞现象.


三 总结

  在这里我们不去说异步的问题了,这个问题需要后面使用线程池和异步任务的时候再说才合适.

  在这里只需要知道Future可以封装线程任务的执行结果.

猜你喜欢

转载自www.cnblogs.com/trekxu/p/9005240.html
022