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. hdu 2817 A sequence of numbers(快速幂)

    Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmeti ...

  2. hdu 5072 Coprime (容斥)

    Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...

  3. HDU--1584--蜘蛛牌--深搜版本号

    蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

  5. C#构造函数的 "继承" 问题

    首先说明下 之所以用 双引号 是因为构造函数是没有继承的 派生类默认会调用基类的无参数构造函数 比如: public class A         { public A()         { Co ...

  6. EF执行存储过程(带输出参数)

    1.不含动态sql.带输出参数存储过程调用实例 1.存储过程代码:   2.EF自动生成代码(包括对应ObjectResult的实体模型): 3.调用存储过程代码实例: 总结: ObjectParam ...

  7. ashx调用session对象

    1.引入命名空间 using System.Web.SessionState 2.必须实现接口 public class Login : IHttpHandler, IRequiresSessionS ...

  8. WndProc函数(转)

    WndProc函数作用: 主要用在拦截并处理系统消息和自定义消息 比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息.这个函数就是默认的消息处理函数.你可以重载这个函数来制定 ...

  9. Visual Studio在页面按F7不能跳转至cs代码页的解决方法

    检查页面Page设置内的CodeBehind属性,看是否与代码页的文件名相同,不同则改正,问题得以解决.

  10. silverlight visifire控件图表制作——silverlight 静态页面xaml

    一.silverlight 静态页面 1. 时间控件:DatePicker ,添加引用: xmlns:sdk="clr-namespace:System.Windows.Controls;a ...