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:

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, ConcurrentHashMaps (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.

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, 2024, Oracle and/or its affiliates. All rights reserved.