concurrent (八) Future

做用:java

  接受多线程的执行结果多线程

全路径:ide

  java.util.concurrentspa

声明:线程

  public interface Future<V> blog

类图结构:get

 

方法it

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的时间就会返回结果

举个例子io

 

  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());
相关文章
相关标签/搜索