It introduces and summarizes Java's concurrency support, shows readers how to initiate, control, and coordinate concurrent activities, and offers numerous recipe-like techniques for designing and implementing Java structures that solve common concurrent programming challenges. The book approaches the topic from a design pattern point of view. All Java programmers interested in doing concurrent programming must understand these new concepts. The book is completely up-to-date with the new thread model that is now incorporated into the most recent version of the Java Virtual Machine. This book offers comprehensive coverage of this vital aspect of the Java language. Thread programming enables developers to design applications that are more responsive to user demands, faster, and more easily controlled. * * Doug Lea * 1.One of Java's most powerful capabilities is its built-in support for concurrent programming, a design technique in which multiple concurrent activities-threads take place inside a single Java program. To simplify usage in most * contexts, this class also defines methods #join()} and * #getNow} that instead throw the CompletionException directly * in these cases. * * In case of exceptional completion with a CompletionException, * methods #get()} and #get(long, TimeUnit)} throw an * ExecutionException} with the same cause as held in the * corresponding CompletionException. Method * #isCompletedExceptionally} can be used to determine if a * CompletableFuture completed in any exceptional fashion. Method #cancel cancel} has the same effect as * completeExceptionally(new CancellationException())}. * * CompletableFuture also implements Future} with the following * policies: * * Since (unlike FutureTask}) this class has no direct * control over the computation that causes it to be completed, * cancellation is treated as just another form of exceptional * completion.
* * All CompletionStage methods are implemented independently of * other public methods, so the behavior of one method is not impacted * by overrides of others in subclasses. To simplify monitoring, * debugging, and tracking, all generated asynchronous tasks are * instances of the marker interface * AsynchronousCompletionTask}. * * All async methods without an explicit Executor * argument are performed using the ForkJoinPool#commonPool()} * (unless it does not support a parallelism level of at least two, in * which case, a new Thread is used). * * In addition to these and related methods for directly * manipulating status and results, CompletableFuture implements * interface CompletionStage} with the following policies: * * Actions supplied for dependent completions of * non-async methods may be performed by the thread that * completes the current CompletableFuture, or by any other caller of * a completion method. * * When two or more threads attempt to * #complete complete}, * #completeExceptionally completeExceptionally}, or * #cancel cancel} * a CompletableFuture, only one of them succeeds.
* However, the following notice accompanied the original version of this * file: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * */ package import import import import import import import import import import import import import import import import import .AtomicInteger import .LockSupport /** * A Future} that may be explicitly completed (setting its * value and status), and may be used as a CompletionStage}, * supporting dependent functions and actions that trigger upon its * completion.
*/ /* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit if you need additional information or have any * questions. * * You should have received a copy of the GNU General Public License version * 2 along with this work if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code).
* * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code.
* * This code is free software you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Line source /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.