Documentation Contents

# Concurrency Utilities Enhancements in Java SE 8

## New classes and interfaces in java.util.concurrent

The `java.util.concurrent` package contains two new interfaces and four new classes:

- Interface `CompletableFuture.AsynchronousCompletionTask`: A marker interface identifying asynchronous tasks produced by async methods.
- Interface `CompletionStage<T>`: A stage of a possibly asynchronous computation, that performs an action or computes a value when another `CompletionStage` completes.
- Class `CompletableFuture<T>`: 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.
- Class `ConcurrentHashMap.KeySetView<K,V>`: A view of a `ConcurrentHashMap` as a `Set` of keys, in which additions may optionally be enabled by mapping to a common value.
- Class `CountedCompleter<T>`: A `ForkJoinTask` with a completion action performed when triggered and there are no remaining pending actions.
- Class `CompletionException`: Exception thrown when an error or other exception is encountered in the course of completing a result or task.

## New methods in java.util.concurrent.ConcurrentHashMap

The Collections Framework has undergone a major revision in Java 8 to add aggregate operations based on the newly added streams facility and lambda expressions. As a result, the `ConcurrentHashMap` class introduces over 30 new methods in this release. These include various `forEach` methods (`forEach`, `forEachKey`, `forEachValue`, and `forEachEntry`), search methods (`search`, `searchKeys`, `searchValues`, and `searchEntries`) and a large number of reduction methods (`reduce`, `reduceToDouble`, `reduceToLong` etc.)

Other miscellaneous methods (`mappingCount` and `newKeySet`) have been added as well. As a result of the JDK 8 changes, `ConcurrentHashMap`s (and classes built from them) are now more useful as caches. These changes include methods to compute values for keys when they are not present, plus improved support for scanning (and possibly evicting) entries, as well as better support for maps with large numbers of elements.

For complete details, see the `java.util.concurrent.ConcurrentHashMap` API specification.

## New classes in java.util.concurrent.atomic

Maintaining a single count, sum, etc. that is updated by possibly many threads is a common scalability problem. This release introduces scalable updatable variable support through a small set of new classes (`DoubleAccumulator`, `DoubleAdder`, `LongAccumulator`, `LongAdder`), which internally employ contention-reduction techniques that provide huge throughput improvements as compared to `Atomic` variables. This is made possible by relaxing atomicity guarantees in a way that is acceptable in most applications.

- `DoubleAccumulator`: One or more variables that together maintain a running double value updated using a supplied function.
- `DoubleAdder`: One or more variables that together maintain an initially zero double sum.
- `LongAccumulator`: One or more variables that together maintain a running long value updated using a supplied function.
- `LongAdder`: One or more variables that together maintain an initially zero long sum.

## New methods in java.util.concurrent.ForkJoinPool

A static `commonPool()` method is now available and appropriate for most applications. The common pool is used by any `ForkJoinTask` that is not explicitly submitted to a specified pool. Using the common pool normally reduces resource usage (its threads are slowly reclaimed during periods of non-use, and reinstated upon subsequent use). Two new methods (`getCommonPoolParallelism()` and `commonPool()`) have been added, which return the targeted parallelism level of the common pool, or the common pool instance, respectively.

## New class java.util.concurrent.locks.StampedLock

A new `StampedLock` class adds a capability-based lock with three modes for controlling read/write access (writing, reading, and optimistic reading). This class also supports methods that conditionally provide conversions across the three modes.

For complete details, see the `java.util.concurrent.StampedLock` API documentation.

Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved.
Contact Us