zoukankan      html  css  js  c++  java
  • concurrent (八) Future

    作用:

      接受多线程的执行结果

    全路径:

      java.util.concurrent

    声明:

      public interface Future<V> 

    类图结构:

     

    方法

    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());
    
  • 相关阅读:
    Python数据类型文件
    Python应用程序之画图本
    opengl 学习笔记1
    圆柱面的法向量
    opengl笔记2(二次曲面的纹理映射)
    正则表达式(一)
    cocos2d的配置
    ogre配置方法
    elipse常用快捷键
    正则表达式(二)
  • 原文地址:https://www.cnblogs.com/amei0/p/9968049.html
Copyright © 2011-2022 走看看