Search

Concurrency API additions - java 8 features


There were some additions to the concurrency API, some of which we will briefly discuss here. ForkJoinPool.commonPool() is the structure that handles all parallel stream operations. The common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool. ConcurrentHashMap has been completley rewritten. StampedLock is a new lock implementation that can be used as an alternative to ReentrantReadWriteLock. CompletableFuture is an implementation of the Future interface that provides methods for performing and chaining asynchronous tasks.


  • ConcurrentHashMap(v8)
  • The following come with forms for parallel, sequentially, Object, int, long, and double. There are too many of these to link, so see theConcurrentHashMap javadocs for more information.
ForkJoinPool.commonPool() is the structure that handles all parallel stream operations. It is intended as an easy, good way to obtain a ForkJoinPool/ExecutorService/Executor when you need one.
ConcurrentHashMap<K, V> was completely rewritten. Internally it looks nothing like the version that was in Java 7. Externally it's mostly the same, except it has a large number of bulk operation methods: many forms of reduce, search, and forEach.
ConcurrentHashMap.newKeySet() provides a concurrent java.util.Set implementation. It is essentially another way of writing Collections.newSetFromMap(new ConcurrentHashMap<T, Boolean>()).
StampedLock is a new lock implementation that can probably replaceReentrantReadWriteLock in most cases. It performs better than RRWL when used as a plain read-write lock. Is also provides an API for "optimistic reads", where you obtain a weak, cheap version of a read lock, do the read operation, then check afterwards if your lock was invalidated by a write. There's more detail about this class and its performance in a set of slides put together by Heinz Kabutz (starting about half-way through the set of slides): "Phaser and StampedLock Presentation"
CompletableFuture<T> is a nice implementation of the Future interface that provides a ton of methods for performing (and chaining together) asynchronous tasks. It relies on functional interfaces heavily; lambdas are a big reason this class was worth adding. If you are currently using Guava's Future utilities, such as Futures,ListenableFuture, and SettableFuture, you may want to check out CompletableFuture as a potential replacement.

No comments:

Post a Comment