Java 9's other new enhancements, Part 6: Concurrency

A publish-subscribe framework for reactive streams, CompletableFuture, and more

1 2 Page 2
Page 2 of 2

Improved support for subclassing

Various enhancements have been made to CompletableFuture to make it easier to extend this class. For example, you might want to override the new Executor defaultExecutor() method to support an alternative default executor.

Another new method that facilitates subclassing CompletableFuture is <U> CompletableFuture<U> newIncompleteFuture(). Check out the JDK 9 documentation for more information on these methods.

New factory methods

Java 8 introduced the <U> CompletableFuture<U> completedFuture(U value) factory method to return a new CompletableFuture that's already completed with the given value. Java 9 complements this method with a new <U> CompletableFuture<U> failedFuture(Throwable ex) method that returns a new CompletableFuture that's already completed exceptionally with the given exception.

Additionally, Java 9 introduces the following pair of completion stage-oriented factory methods into CompletedFuture to return completed and completed exceptionally completion stages:

  • <U> CompletionStage<U> completedStage(U value): Returns a new CompletionStage that's already completed with the given value and supports only those methods in interface CompletionStage.
  • <U> CompletionStage<U> failedStage(Throwable ex): Returns a new CompletionStage that's already completed exceptionally with the given exception and supports only those methods in interface CompletionStage.

Additional concurrency improvements

Numerous implementation improvements that have accumulated since Java 8 have been introduced in Java 9. Many of these improvements are small; some improvements consist of Javadoc rewordings.

For example, the following new constructor for creating a fork/join pool has been added to the java.util.concurrent.ForkJoinPool class:

ForkJoinPool(int parallelism,
             ForkJoinPool.ForkJoinWorkerThreadFactory factory,
             Thread.UncaughtExceptionHandler handler,
             boolean asyncMode,
             int corePoolSize,
             int maximumPoolSize,
             int minimumRunnable,
             Predicate<? super ForkJoinPool> saturate,
             long keepAliveTime,
             TimeUnit unit)

Also, the java.util.concurrent.atomic.AtomicReferenceArray<E> class's boolean weakCompareAndSet(int i, E expectedValue, E newValue) method has been deprecated and the following new methods have been added:

E compareAndExchange(int i, E expectedValue, E newValue)
E compareAndExchangeAcquire(int i, E expectedValue, E newValue)
E compareAndExchangeRelease(int i, E expectedValue, E newValue)
E getAcquire(int i)
E getOpaque(int i)
E getPlain(int i)
void setOpaque(int i, E newValue)
void setPlain(int i, E newValue)
void setRelease(int i, E newValue)
E weakCompareAndSetAcquire(int i, E expectedValue, E newValue)
boolean weakCompareAndSetPlain(int i, E expectedValue, E newValue)
E weakCompareAndSetRelease(int i, E expectedValue, E newValue)
E weakCompareAndSetVolatile(int i, E expectedValue, E newValue)

Conclusion

The continual evolution of concurrency and parallelism usage in applications requires the continual evolution of library support. Java 9 has made great strides in this direction through its publish-subscribe framework, CompletableFuture enhancements, and additional concurrency improvements to ForkJoinPool and other classes.

download
Download the source code for this post's example applications. Created by Jeff Friesen for JavaWorld.

The following software was used to develop the post's code:

  • 64-bit JDK 9ea+154

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 8.1
1 2 Page 2
Page 2 of 2