what are Key classes in java.util.concurrent package
Carvia Tech | May 25, 2019 | 2 min read | 446 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
- What is difference between JDK JRE and JVM
- Can the keys in HashMap be mutable
- What is AtomicInteger class and how it works internally
- What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications
- Design an Immutable class that has an java.util.Date member
- What is volatile keyword in Java
Find more on this topic:
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:
- Submit Form with Java 11 HttpClient - Kotlin
- Explain Java Exception Class Hierarchy
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Set precision and scale for a double value in java
- Difference between HashMap, LinkedHashMap and TreeMap
- What is difference between ExecutorService submit and execute method
- What is left shift right shift and unsigned rght shift operator in Java
- What happens when wait() & notify() method are called
- can we write a java method that swaps two integers