Future类存在于JDK的concurrent包中,主要用途是接收Java的异步线程计算返回的结果。

个人理解的使用场景大概如下:

有两个任务A和B,A任务中仅仅需要使用B任务计算成果,有两种方法实现:

  1. A和B在同一个线程中顺序执行。即先执行B,得到返回结果之后再执行A。
  2. 开两个线程,A和B并行执行。当A需要B的计算结果时如果B还没有执行完,A既可以选择阻塞等待B执行完,也可以先做其他的工作,过一段时间后再询问一次B。

毫无疑问,如果B是一个耗时比较大的计算任务时,后者比前者的效率高了很多。

使用Java的Future对象的实现。

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; public class FutureDemo { public static void main(String[] args) { ExecutorService threadPool = Executors.newCachedThreadPool();
Future<Integer> future = threadPool.submit(new Callable<Integer>() {
public Integer call() throws Exception {
Thread.sleep(3000);
return new Random().nextInt(100);
}
}); doSomething(); try {
System.out.println("is B done : " + future.isDone());
System.out.println("result of B : " + future.get());
System.out.println("is B done : " + future.isDone());
int result = future.get(); doSomethingWithB(result);
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
System.exit(0);
} private static void doSomethingWithB(int result) {
// TODO Auto-generated method stub } private static void doSomething() {
// TODO Auto-generated method stub } }

输出结果:

is B done : false
result of B : 25
is B done : true

Future对象使用get方法获取线程的返回值,即call方法的返回值。

不带参数的get方法是阻塞方法,只要线程为返回结果就会一直阻塞直到有结果为止。

可以使用isDone方法判断线程是否结束。也可以使用带参数的get方法,若指定时间内还没有得到线程返回值,会抛出TimeoutException的异常。

该方法源码:

/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an
* exception
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws TimeoutException if the wait timed out
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;

Java异步调用Future对象的更多相关文章

  1. 说说Java异步调用的几种方式

    日常开发中,会经常遇到说,前台调服务,然后触发一个比较耗时的异步服务,且不用等异步任务的处理结果就对原服务进行返回.这里就涉及的Java异步调用的一个知识.下面本文尝试将Java异步调用的多种方式进行 ...

  2. 5种必会的Java异步调用转同步的方法你会几种

    转载请注明本文地址:https://www.jianshu.com/p/f00aa6f66281 源码地址:https://gitee.com/sunnymore/asyncToSync Sunny先 ...

  3. Java异步调用转同步的5种方式

    1.异步和同步的概念 同步调用:调用方在调用过程中,持续等待返回结果. 异步调用:调用方在调用过程中,不直接等待返回结果,而是执行其他任务,结果返回形式通常为回调函数. 2 .异步转为同步的概率 需要 ...

  4. java异步调用方法

    一.利用多线程 直接new线程 Thread t = new Thread(){ @Override public void run() { longTimeMethod(); } }; 使用线程池 ...

  5. java异步计算Future的使用(转)

    从jdk1.5开始我们可以利用Future来跟踪异步计算的结果.在此之前主线程要想获得工作线程(异步计算线程)的结果是比较麻烦的事情,需要我们进行特殊的程序结构设计,比较繁琐而且容易出错.有了Futu ...

  6. Dubbo消费者异步调用Future使用

    Dubbo的四大组件工作原理图,其中消费者调用提供者采用的是同步调用方式.消费者对于提供者的调用,也可以采用异步方式进行调用.异步调用一般应用于提供者提供的是耗时性IO服务 一.Future异步执行原 ...

  7. java 异步调用与多线程

    异步与多线程的区别 一.异步和多线程有什么区别?其实,异步是目的,而多 线程是实现这个目的的方法.异步是说,A发起一个操作后(一般都是比较耗时的操作,如果不耗时的操作 就没有必要异步了),可以继续自顾 ...

  8. java反射调用某个对象的方法

    // 反射调用某个对象的方法 public Object invokeMethod(Object methodObject, String methodName, Object[] args) thr ...

  9. 从Java future 到 Guava ListenableFuture实现异步调用

    从Java future 到 Guava ListenableFuture实现异步调用 置顶 2016年04月24日 09:11:14 皮斯特劳沃 阅读数:17570 标签: java异步调用线程非阻 ...

随机推荐

  1. JavaScript进阶篇 - -第1章 系好安全带

    第1章 系好安全带 html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", ...

  2. 学完 JAVA SE后学什么 。。。

    我觉得学习j2ee一定要循序渐进,千万不要太急了.把java基础打牢一点,再牢一点.各位,你们在后面学习什么 struts,hibernate,spring,ajax..都很轻松. 第一个阶段(jav ...

  3. hdu 5012 Dice

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  4. js高级程序设计(第三版)学习笔记(第一版)

    ecma:欧洲计算机制造商协会iso/iec:国际标准化和国际电工委员会 dom级别(10*)文档对象模型1:DOM核心(映射基于xml文档)与dom html(在dom核心基础上)2:对鼠标,事件, ...

  5. js练习【DOM操作】

    完成效果: 演示地址:http://codepen.io/anon/pen/jPbYWq HTML: <!DOCTYPE html> <html lang="en" ...

  6. 什么是 gnuplot

    Gnuplot是一个命令行的交互式绘图工具(command-driven interactive function plotting program).用户通过输入命令,可以逐步设置或修改绘图环境,并 ...

  7. oracle的启动和关闭

    一.sql*plus方式:      用sql*plus来连接到Oracle Sqlplus /nolog 是以不连接数据库的方式启动sql*plus Connect /as sysdba 是以DBA ...

  8. compass模块----Utilities

    引入Utilities: @import "compass/utilities"; 分别引入: @import "compass/utilities/color" ...

  9. (转)sql中 in 、not in 、exists、not exists 用法和差别

    exists (sql 返回结果集为真)  not exists (sql 不返回结果集为真)  如下:  表A  ID NAME  1    A1  2    A2  3  A3 表B  ID AI ...

  10. Key lock 的秘密

    研究死锁,或者观察sp_lock,有时候最恼人的莫过于你看到下面研究成果的key lock,但是却不知道究竟是哪个page 哪个row被lock住了: Exec sp_lock:   就说上面的key ...