Concurrency Utilities in Java
Java 5 introduced the java.util.concurrent package, which provides high-level concurrency utilities that make it easier to write correct, efficient multith...
The java.util.concurrent Package
Java 5 introduced the java.util.concurrent package, which provides high-level concurrency utilities that make it easier to write correct, efficient multithreaded code. Before this package existed, developers had to build everything from scratch using the low-level synchronized and wait/notify mechanisms, which are error-prone. The package is organized into several categories: executor services, concurrent collections, synchronizers, and atomic variables.
ExecutorService abstracts away thread creation and lifecycle management. You submit tasks as Runnable or Callable objects, and the framework handles dispatching them to a pool of worker threads. This separation between task definition and execution is a foundational design principle that makes code easier to test and maintain.
CountDownLatch allows one or more threads to wait until a set of operations completes. You initialize it with a count, decrement it from worker threads using countDown(), and wait in the main thread using await(). When the count reaches zero, all waiting threads are released:
CountDownLatch latch = new CountDownLatch(3); for (int i = 0; i < 3; i++) { pool.submit(() -> { doWork(); latch.countDown(); }); } latch.await(); // blocks until all 3 tasks call countDown()
CyclicBarrier is similar but reusable. A group of threads each call await() on the barrier and all block until every thread has arrived, at which point they are all released together. This is useful for parallel computations that must synchronize at checkpoints.
Semaphore controls access to a limited number of resources. Initializing a Semaphore with n means at most n threads can hold a permit at the same time. Calling acquire() blocks until a permit is available; release() returns one. This is the concurrency utility equivalent of a bouncer controlling entry to a venue with a capacity limit.
BlockingQueue provides thread-safe producer-consumer queues where producers block when the queue is full and consumers block when it is empty, eliminating the need to implement this coordination manually.
Interview tip: Explain the difference between CountDownLatch and CyclicBarrier. A CountDownLatch cannot be reset once it reaches zero, while a CyclicBarrier can be reused after all parties have arrived, making it better suited for iterative parallel algorithms.
Executors and Thread Pools
The Executors factory class provides static methods for creating common thread pool configurations without writing pool management code from scratch. Choosing the right pool type has significant implications for application throughput, latency, and resource usage.
newFixedThreadPool(n) creates a pool with exactly n threads. Tasks beyond n are queued in an unbounded LinkedBlockingQueue and executed as threads become free. This configuration provides predictable memory usage for the thread pool itself and is appropriate for CPU-bound tasks where you want to limit parallelism to the number of available cores.
newCachedThreadPool() creates threads on demand and caches idle threads for 60 seconds before terminating them. If all threads are busy when a new task arrives, a fresh thread is created immediately. This pool is ideal for short-lived, bursty workloads but can create an unbounded number of threads if tasks arrive faster than they complete, which may exhaust system resources.
newScheduledThreadPool(n) supports delayed and periodic task execution, replacing Timer and TimerTask for scheduling in concurrent applications. Tasks can be scheduled with a fixed delay between completions or at a fixed rate regardless of how long each execution takes.
For maximum control, construct a ThreadPoolExecutor directly:
ThreadPoolExecutor pool = new ThreadPoolExecutor( 2, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy() );
The parameters specify: core threads (always kept alive), maximum threads, keep-alive time for idle excess threads, the task queue, and the rejection policy. The CallerRunsPolicy causes the submitting thread to run the task itself when the queue is full, providing backpressure rather than throwing an exception.
Java 7 introduced ForkJoinPool, designed for divide-and-conquer algorithms using work stealing, where idle threads steal tasks from busy threads' queues. Java 8's parallel streams use the common ForkJoinPool by default.
Interview tip: Describe why newCachedThreadPool() can be dangerous in production. If a downstream service becomes slow, tasks queue up faster than they complete, the pool creates thousands of threads, memory and CPU are exhausted, and the application crashes. A bounded queue with an explicit rejection policy is safer.
Concurrent Collections
Standard Java collections like ArrayList, HashMap, and LinkedList are not thread-safe. Wrapping them with Collections.synchronizedList() or synchronizedMap() adds coarse synchronization but creates a bottleneck because every operation acquires the same lock. The concurrent collections in java.util.concurrent provide finer-grained concurrency for significantly better throughput in multithreaded scenarios.
ConcurrentHashMap is the most commonly used concurrent collection. In Java 7 and earlier, it divided the map into segments, each with its own lock. In Java 8, it switched to a more granular bucket-level locking and CAS operations, allowing concurrent reads without any locking at all and concurrent writes to different buckets without blocking each other. For read-heavy workloads, ConcurrentHashMap outperforms synchronized alternatives by a wide margin.
CopyOnWriteArrayList is a thread-safe list optimized for read-heavy scenarios where writes are rare. Every write operation creates a fresh copy of the underlying array, applies the modification to the copy, and atomically swaps in the new array. Reads require no locking at all because they operate on a snapshot. This makes iteration safe even when another thread modifies the list concurrently, but writes are expensive because of the array copy.
ConcurrentLinkedQueue provides a non-blocking, lock-free queue suitable for high-throughput producer-consumer scenarios. It uses CAS operations to maintain consistency without locks, making it extremely efficient when contention is high.
LinkedBlockingQueue and ArrayBlockingQueue are blocking queue implementations that block producers when the queue is full and consumers when it is empty. They are the backbone of ExecutorService task queues and manual producer-consumer pipelines.
Example using ConcurrentHashMap:
ConcurrentHashMap<String, Integer> wordCount = new ConcurrentHashMap<>(); wordCount.merge("hello", 1, Integer::sum);
The merge() method atomically combines the new value with the existing value using the provided function, eliminating the read-modify-write race condition that would occur with a regular HashMap.
Interview tip: When asked about ConcurrentHashMap vs Hashtable, explain that Hashtable synchronizes every method on a single lock, serializing all access. ConcurrentHashMap allows concurrent reads and fine-grained concurrent writes, providing much higher throughput in practice.