作用:

  接受多线程的执行结果

全路径:

  java.util.concurrent

声明:

  public interface Future<V> 

类图结构:

 concurrent (八) Future

方法

boolean cancel(boolean mayInterruptIfRunning);//方法可以用来停止一个任务,如果任务可以停止(通过mayInterruptIfRunning来进行判断),则可以返回true,如果任务已经完成或者已经停止,或者这个任务无法停止,则会返回false.
boolean isCancelled(); //判断当前方法是否取消
boolean isDone(); //判断当前方法是否完成
V get() throws InterruptedException, ExecutionException; // 方法可以当任务结束后返回一个结果,如果调用时,工作还没有结束,则会阻塞线程,直到任务执行完毕
V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException; //做多等待timeout的时间就会返回结果

举个例子

 

  Callable callable=new Callable() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000*5);
                return "233";
            }
        };
        FutureTask<String> ft=new FutureTask<>(callable);
        new Thread(ft).start();
        System.out.println(ft.get());

 

相关文章:

  • 2021-12-06
  • 2021-03-31
  • 2021-11-21
  • 2021-08-23
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
猜你喜欢
  • 2022-02-20
  • 2022-12-23
  • 2021-06-08
  • 2021-08-10
  • 2022-01-13
  • 2021-05-27
  • 2021-09-17
相关资源
相似解决方案