what are Key classes in java.util.concurrent package

Carvia Tech | May 25, 2019 | 2 min read | 205 views


java.util.concurrent package holds utility classes that are commonly useful in concurrent programming.

At a high level this package contains concurrent utilities like Executors, Queues, Timing, Synchronizers and Concurrent Collections.

Executors

Executor is a simple interface for defining custom thread-like subsystems, including thread pools, asynchronous I/O, and light weight task frameworks. ExecutorService provides a more complete asynchronous task execution framework.

Implementation classes include:

Executors

Executors class provides factory methods for most common kind of configurations of Executors.

ThreadPoolExecutor

Provides tunable and flexible thread pools.

ForkJoinPool

uses work-stealing scheduler for high throughput computation intensive parallel processing

ExecutorCompletionService

that assists in coordinating the processing of groups of asynchronous tasks.

Queues

ConcurrentLinkedQueue

The ConcurrentLinkedQueue class supplies an efficient scalable thread-safe non-blocking FIFO queue. The ConcurrentLinkedDeque class is similar, but additionally supports the java.util.Deque interface.

LinkedTransferQueue

LinkedTransferQueue introduce a synchronous transfer method (along with related features) in which a producer may optionally block awaiting its consumer.

There are five other implementations - LinkedBlockingQueue, ArrayBlockingQueue, SynchronousQueue, PriorityBlockingQueue and DelayQueue.

Synchronizers

Five special purpose synchronizers are present in this package.

Semaphore

It is a classic concurrency tool which is often used to restric the number of threads that can access a shared resource (logical or physical).

CountDownLatch

It is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.

CyclicBarrier

It is a resettable multiway synchronization point useful in some styles of parallel programming.

Exchanger

It allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs.

Phaser

It provides a more flexible form of barrier that may be used to control phased computation among multiple threads.

Concurrent Collections

This package supplies Collection implementations designed for use in multithreaded contexts:

  • ConcurrentHashMap

  • ConcurrentSkipListMap

  • ConcurrentSkipListSet

  • CopyOnWriteArrayList

  • CopyOnWriteArraySet

When many threads are expected to access a given collection, a ConcurrentHashMap is normally preferable to a synchronized HashMap, and a ConcurrentSkipListMap is normally preferable to a synchronized TreeMap.

A CopyOnWriteArrayList is preferable to a synchronized ArrayList when the expected number of reads and traversals greatly outnumber the number of updates to a list.

A concurrent collection is thread-safe, but not governed by a single exclusion lock. In the particular case of ConcurrentHashMap, it safely permits any number of concurrent reads as well as a tunable number of concurrent writes. "Synchronized" classes can be useful when you need to prevent all access to a collection via a single lock, at the expense of poorer scalability. In other cases in which multiple threads are expected to access a common collection, "concurrent" versions are normally preferable. And unsynchronized collections are preferable when either collections are unshared, or are accessible only when holding other locks.

Iterators and Spliterators returned by most concurrent Collection implementations provide weakly consistent rather than fail-fast traversal. That means:

  • they may proceed concurrently with other operations

  • they will never throw ConcurrentModificationException

  • they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.


Top articles in this category:
  1. What is difference between JDK JRE and JVM
  2. What is Immutable Class. Why would you choose it? How would you make a class immutable?
  3. Can the keys in HashMap be mutable
  4. What is AtomicInteger class and how it works internally
  5. Design an Immutable class that has an java.util.Date member
  6. What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications
  7. What is volatile keyword in Java



Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info