what are Key classes in java.util.concurrent package
Upasana | May 25, 2019 | 2 min read | 551 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.
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:
Executorsclass provides factory methods for most common kind of configurations of Executors.
Provides tunable and flexible thread pools.
uses work-stealing scheduler for high throughput computation intensive parallel processing
that assists in coordinating the processing of groups of asynchronous tasks.
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 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.
Five special purpose synchronizers are present in this package.
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).
It is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.
It is a resettable multiway synchronization point useful in some styles of parallel programming.
It allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs.
It provides a more flexible form of barrier that may be used to control phased computation among multiple threads.
This package supplies Collection implementations designed for use in multithreaded contexts:
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:
- What is Immutable Class in Java
- Can the keys in HashMap be mutable
- Design an Immutable class that has an java.util.Date member
- What is AtomicInteger class and how it works internally
- Difference between JDK JRE and JVM
- What is volatile keyword in Java
- What are four principles of OOP, How aggregation is different than Composition?