java.util.concurrent.Future Basics

Hereby I am starting a series of articles about future concept in programming languages (also known as promises or delays) with a working title: Back to the Future. Futures are very important abstraction, even more these day than ever due to growing demand for asynchronous, event-driven, parallel and scalable systems. In the first article we‘ll discover most basic java.util.concurrent.Future<T> interface. Later on we will jump into other frameworks, libraries or even languages. Future<T> is pretty limited, but essential to understand, ekhm, future parts.

In a single-threaded application when you call a method it returns only when the computations are done (IOUtils.toString() comes from Apache Commons IO):

public String downloadContents(URL url) throws IOException {
    try(InputStream input = url.openStream()) {
        return IOUtils.toString(input, StandardCharsets.UTF_8);
    }
}
//...
final String contents = downloadContents(new URL("http://www.example.com"));

downloadContents() looks harmless1, but it can take even arbitrary long time to complete. Moreover in order to reduce latency you might want to do other, independent processing in the meantime, while waiting for results. In the old days you would start a new Thread and somehow wait for results (shared memory, locks, dreadful wait()/notify() pair, etc.) With Future<T> it‘s much more pleasant:

public static Future<String> startDownloading(URL url) {
    //...
}
final Future<String> contentsFuture = startDownloading(new URL("http://www.example.com"));
//other computation
final String contents = contentsFuture.get();

We will implement startDownloading() soon. For now it‘s important that you understand the principles. startDownloading() does not block, waiting for external website. Instead it returns immediately, returning a lightweight Future<String> object. This object is a promise that String will be available in the future. Don‘t know when, but keep this reference and once it‘s there, you‘ll be able to retrieve it using Future.get(). In other words Future is a proxy or a wrapper around an object that is not yet there. Once the asynchronous computation is done, you can extract it. So what API does Future provide?

Future.get() is the most important method. It blocks and waits until promised result is available (resolved). So if we really need that String, just call get() and wait. There is an overloaded version that accepts timeout so you won‘t wait forever if something goes wild. TimeoutException is thrown if waiting for too long.

In some use cases you might want to peek on the Future and continue if result is not yet available. This is possible with isDone().
Imagine a situation where your user waits for some asynchronous
computation and you‘d like to let him know that we are still waiting and
do some computation in the meantime:

final Future<String> contentsFuture = startDownloading(new URL("http://www.example.com"));
while (!contentsFuture.isDone()) {
    askUserToWait();
    doSomeComputationInTheMeantime();
}
contentsFuture.get();

The last call to contentsFuture.get() is guaranteed to return immediately and not block because Future.isDone() returned true. If you follow the pattern above make sure you are not busy waiting, calling isDone() millions of time per second.

Cancelling futures is the last aspect we have not covered yet. Imagine
you started some asynchronous job and you can only wait for it given
amount of time. If it‘s not there after, say, 2 seconds, we give up and
either propagate error or work around it. However if you are a good
citizen, you should somehow tell this future object: I no longer need
you, forget about it. You save processing resources by not running
obsolete tasks. The syntax is simple:

contentsFuture.cancel(true);    //meh...

We all love cryptic, boolean parameters, aren‘t we? Cancelling comes in two flavours. By passing false to mayInterruptIfRunning parameter we only cancel tasks that didn‘t yet started, when the Future represents results of computation that did not even began. But if our Callable.call() is already in the middle, we let it finish. However if we pass true, Future.cancel() will be more aggressive, trying to interrupt already running jobs as well. How? Think about all these methods that throw infamous InterruptedException, namely Thread.sleep(), Object.wait(), Condition.await(), and many others (including Future.get()). If you are blocking on any of such methods and someone decided to cancel your Callable, they will actually throw InterruptedException, signalling that someone is trying to interrupt currently running task.



So we now understand what

Future<T>

is - a place-holder for something, that you will get in the
future. It‘s like keys to a car that was not yet manufactured. But how
do you actually obtain an instance of

Future<T>

in your application? Two most common sources are thread
pools and asynchronous methods (backed by thread pools for you). Thus
our

startDownloading()

method can be rewritten to:

private final ExecutorService pool = Executors.newFixedThreadPool(10);
public Future<String> startDownloading(final URL url) throws IOException {
    return pool.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            try (InputStream input = url.openStream()) {
                return IOUtils.toString(input, StandardCharsets.UTF_8);
            }
        }
    });
}

A lot of syntax boilerplate, but the basic idea is simple: wrap long-running computations in

Callable<String>

and

submit()

them to a thread pool of 10 threads. Submitting returns some implementation of

Future<String>

, most likely somehow linked to your task and thread pool. Obviously your task is not executed immediately. Instead it is placed in a queue which is later (maybe even much later) polled by thread from a pool. Now it should be clear what these two flavours of

cancel()

mean - you can always cancel task that still resides in that queue. But cancelling already running task is a bit more complex.

Another place where you can meet

Future

is Spring and EJB. For example in Spring framework you can simply annotate your method with @Async:

@Async
public Future<String> startDownloading(final URL url) throws IOException {
    try (InputStream input = url.openStream()) {
        return new AsyncResult<>(
                IOUtils.toString(input, StandardCharsets.UTF_8)
        );
    }
}

Notice that we simply wrap our result in AsyncResult implementing

Future

. But the method itself does not deal with thread pool or asynchronous processing. Later on Spring will proxy all calls to

startDownloading()

and run them in a thread pool. The exact same feature is available through @Asynchronous annotation in EJB.

So we learned a lot about

java.util.concurrent.Future

. Now it‘s time to admit - this interface is quite limited, especially when compared to other languages. More on that later.

时间: 2024-11-10 11:31:31

java.util.concurrent.Future Basics的相关文章

java.util.concurrent 多线程框架

http://daoger.iteye.com/blog/142485 博客分类: 技术研究 多线程Java框架Tomcatthread (来源于http://www.zhuaxia.com/item/590227619/) JDK5中的一个亮点就是将Doug Lea的并发库引入到Java标准库中.Doug Lea确实是一个牛人,能教书,能出书,能编码,不过这在国外还是比较普遍的,而国内的教授们就相差太远了. 一般的服务器都需要线程池,比如Web.FTP等服务器,不过它们一般都自己实现了线程池,

jdk 1.8 java.util.concurrent 学习(一)

java.util.concurrent主要分为五个部分: atomic数据类型 同步锁 并发容器 多线程任务执行 线程管理 1. atomic(原子) 数据类型 放在java.util.concurrent.atomic这个包里面,实现了原子化操作的数据类型,包括 Boolean, Integer, Long, 和Referrence(引用)这四种类型以及这四种类型的数组类型 2. 同步锁 在java.util.concurrent.lock这个包里面,实现了并发操作中的几种类型的锁 3. j

[转载] java多线程学习-java.util.concurrent详解(二)Semaphore/FutureTask/Exchanger

转载自http://janeky.iteye.com/blog/770393 ----------------------------------------------------------------------------- 3. Semaphore     我们先来学习一下JDK1.5 API中关于这个类的详细介绍: “一个计数信号量.从概念上讲,信号量维护了一个许可集.如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可.每个 release() 添加一个许可,从

Jakob Jenkov 写的 java.util.concurrent API 指南

1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Java 的并发编程变得更加简单轻松的类.在这个包被添加以前,你需要自己去动手实现自己的相关工具类.本文我将带你一一认识 java.util.concurrent 包里的这些类,然后你可以尝试着如何在项目中使用它们.本文中我将使用 Java 6 版本,我不确定这和 Java 5 版本里的是否有一些差异.

《Java并发编程实战》要点笔记及java.util.concurrent 的结构介绍

买了<java并发编程实战>这本书,看了好几遍都不是很懂,这个还是要在实战中找取其中的要点的,后面看到一篇文章笔记做的很不错分享给大家!! 原文地址:http://blog.csdn.net/cdl2008sky/article/details/26377433 Subsections  1.线程安全(Thread safety) 2.锁(lock) 3.共享对象 4.对象组合 5.基础构建模块 6.任务执行 7.取消和关闭 8.线程池的使用 9.性能与可伸缩性 10.并发程序的测试 11.显

java多线程学习--java.util.concurrent

CountDownLatch,api 文档:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. 假设我们要打印1-100,最

java.util.concurrent介绍

(本文由 blog博主Caoer(草儿)原创,此处为转载. ) java.util.concurrent 包含许多线程安全.测试良好.高性能的并发构建块.不客气地说,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结构所执行的并发操作.通过提供一组可靠的.高性能并发构建块,开发人员可以提高并发类的线程安全.可伸缩性.性能.可读性和可靠性. 如果一些类名看起来相似,可能是因为 java.util.concurrent 中的许多概念源自 Doug L

Java 并发工具包 java.util.concurrent 大全

1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Java 的并发编程变得更加简单轻松的类.在这个包被添加以前,你需要自己去动手实现自己的相关工具类. 本文我将带你一一认识 java.util.concurrent 包里的这些类,然后你可以尝试着如何在项目中使用它们.本文中我将使用 Java 6 版本,我不确定这和 Java 5 版本里的是否有一些差异

Java Concurrency - java.util.concurrent API Class Diagram

摘自: www.uml-diagrams.org Here we provide several UML class diagrams for the Java™ 7 java.util.concurrent package. Several java.util.concurrent.* packages introduced with version 5.0 of the Java platform added high-level concurrency features to the Ja