Home » Core Java » util » concurrent


Java ScheduledExecutorService Example

1. Introduction It is common requirement in software development that we want tasks to run (i) periodically or (ii) start after a fixed delay. Since Java is known for richness of its development platform, it abstracts such requirements in an interface java.util.concurrent.ScheduledExecutorService and its concrete implementation java.util.concurrent.ScheduledThreadPoolExecutor In this post we would not just look into these APIs but also ...

Read More »

Java CompletionService Example

1. Introduction to CompletionService The Java JDK provides some “services” for common use cases. The asynchronous Producer-Consumer pattern is one such common use case which is often required in day to day software development. As such the JDK provides platform level support in the form of APIs like java.util.concurrent.CompletionService and java.util.concurrent.ExecutorCompletionService. CompletionService abstracts the service of handling tasks which could ...

Read More »

Java CompletionStage and CompletableFuture Example

1. Introduction Java JDK8 included the big fat interface called CompletionStage in the java.util.concurrent package. The same package also contains CompletableFuture which is a library implementation of CompletionStage. In this post we would see how CompletionStage and CompletableFuture provide piped asynchronous API thus enhancing reactive programming support in Java at the platform level. Since we are talking about JDK8 APIs, ...

Read More »

java.util.concurrent.ConcurrentSkipListMap Example

ConcurrentSkipListMap is a scalable concurrent navigable map implementation. The map is sorted based on natural ordering of its keys or by comparator depending on the usage of constructor. ConcurrentSkipListMap  class implements a concurrent  variant of Skip lists which provides expected average log(n) time cost for the get, put, containsKey and remove operations. These methods are multiple thread based  and safe ...

Read More »

Java ReentrantReadWriteLock Example

This is an example of how to make use of the ReentrantReadWriteLock class of Java. It is an implementation of ReadWriteLock, that also supports ReentrantLock functionality. The ReadWriteLock is a pair of associated Locks, one for read-only operations and one for writing. Whereas, the ReentrantLock is a reentrant mutual exclusion Lock with the same behavior as the implicit monitor lock accessed using synchronized methods ...

Read More »

java.util.concurrent.RejectedExecutionHandler Example

Here we will discuss about the RejectedExecutionHandler Interface in the java.util.concurrent package. This interface is really helpfull when working with the ThreadPoolExecutor. 1. Overview & Usage The interface java.util.concurrent.RejectedExecutionHandler is a handler for tasks that cannot be executed by a ThreadPoolExecutor ( a class that implements ExecutorService, ). This may occur when no more threads or queue slots are available ...

Read More »

java.util.concurrent.ThreadFactory Example

In this example we shall show you how to make use ThreadFactory interface, ThreadFactory is implemented by a user class to override its newThread() method for on-demand creation of a new thread with a specific configuration like thread name, type(user, daemon) and priority, etc.         ThreadFactory vs default ThreadFactory: In a typical Java ExecutorService application where some threads ...

Read More »

java.util.concurrent.locks.AbstractQueuedSynchronizer Example

AbstractQueuedSynchronizer class has support for exclusive and shared mode of locks and synchronizers. It provides methods for inspection, instrumentation and monitoring methods for condition objects. To implement, the following methods for releasing, acquiring shared, releasing shared, exclusively holding, getting state, setting state and comparing state have to be reimplemented:           tryAcquire(int) tryRelease(int) tryAcquireShared(int) tryReleaseShared(int) isHeldExclusively() getState() ...

Read More »

java.util.concurrent.ConcurrentNavigableMap Example

In this example we shall show you how to make use ConcurrentNavigableMap Interface, ConcurrentNavigableMap is a NavigableMap which provides navigation methods that returns the closest match for given search targets with a concurrent access support for its submaps. The submaps are the maps returned by various methods like headMap(K toKey), tailMap(K fromKey) and subMap(K fromKey, K toKey).       ConcurrentNavigableMap ...

Read More »

java.util.concurrent.atomic.AtomicLongArray Example

In this example we shall show you how to make use of AtomicLongArray class,  AtomicLongArray is a long array in which elements may be updated atomically. In a multithreading concurrent application architecture, we have a few tasks which are parallelized in such a way as to require no coordination between threads. The true problem comes when we need a coordination between ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns