Skip to main content

Concurrent Collection Classes:

Concurrent Collection Classes:

Java utility classes are generally useful when used with concurrent programming. This package includes a few small standardized extensible frameworks and some classes that provide proper functionality that is otherwise very difficult to implement.


Concurrent Collections:

This package supplies Collection implementations mainly used in multithreaded processes. A concurrent collection is always thread-safe, and a single exclusion lock does not govern it.


Here are some of the classes below:

  • ConcurrentSkipListMap: Usually preferable to a synchronized TreeMap

  • CopyOnWriteArraySet: Well suited for applications where set sizes generally stay small and read-only operations vastly outnumber mutative operations(add, set, remove, etc.).

  • ConcurrentSkipListSet: This is essentially an equivalent of TreeMap and TreeSet for concurrent code.

  • CopyOnWriteArrayList: It’s preferable over synchronized ArrayList, where an expected number of reads vastly outnumbers the number of updates in the list.

  • ConcurrentHashMap: Whenever many threads are expected to access a given collection, ConcurrentHashMap usually is preferable to a synchronized HashMap.

Popular posts from this blog

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

What is Deadlock, Livelock and Starvation?

Deadlock, Livelock and Starvation: Deadlock: Deadlock   is where a set of processes are blocked because each process is holding a resource and waiting for another resource that is doing the same thing.  An example of this is when two trains are coming toward each other on the same track. Neither of the trains can move once they are in front of each other (assuming there are no connecting tracks).  Livelock: Livelock  occurs when two or more processes continue to repeat in response to changes in other processes without achieving any productive outcome. These processes do not get placed in the waiting state but continue to run without any productive outcome. This differs from a deadlock because, in a deadlock, all processes are put in the waiting state. Starvation: Starvation is a problem that is closely related to both Livelock and Deadlock. This can occur as a policy is used to determine who gets access to the resource when it can lead to some processes never getting...

Atomic operation and classes in the Java Concurrency API:

Atomic Operation and Class: An operation is classified as atomic if it is performed as a single unit of work without the possibility of interference from other processes. In the Java language specification, they guarantee that the reading or writing operation is atomic (unless the variable is a long or a double). Variables of type long or double in operations are only atomic if declared with the keyword: volatile. Suppose we assume that  i  is defined as an int. The  i++  (increment) operation is not an atomic operation in Java. This also applies to the other numeric types, e.g. long.  This is because the  i++  operation first reads the value currently stored in  i , and then it adds one to it. However, between the read and the write, the value of  i  might have changed. Since Java 1.5, the java language provides an atomic class with variables such as AtomicInteger or AtomicLong. The class also provides methods like getAndDecrement(), ge...