Concurrent Collection in Java

Concurrent Collection

In Synchronized Collection a thread lock the entire list. Only one thread access the entire list at a time. In concurrent collection a threat lock one element, each  element can be accessed by different thread concurrently 



Java supported multithreading and concurrency from initiation. Java has introduced the concurrent collection in java 5.0 and java 6.0 over the synchronized collection to improve the performance in a multithreading environment. java has provided concurrent collection API apart from collection API to handle the multithreading and concurrency in a better manner. We can find all the new concurrent collection Interfaces and Classes under the package name java.util.concurrent.


In this article, we will focus on some Key interfaces and classes of the java.util.concurrent package.

Synchronized Collection V/S Concurrent Collection:

Synchronized Collection:

  • Synchronized Collections are slow in performance 
  • Multiple threads can access a synchronized collection sequentially. Only one thread can access the collection at a time.
  • Synchronized Collection locks the entire collection. 
  • Permits null values.
  • Static synchronized methods of Collections class are used to make a collection synchronize.

Concurrent Collection

  • Concurrent Collections are fast in performance 
  • Multiple threads can access a concurrent collection concurrently. Multiple threads can access the collection at a time.
  • Concurrent Collection does not lock the entire collection, it does lock only one element at a time. In other words, each element has a separate lock. 
  • It does not permit null values.

Concurrent Collection Key Interfaces:

Interface

Description

BlockinDeque

A Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.

BlockingQueue

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

ConcurrentMap

A Map providing thread safety and atomicity guarantees

ConcurrentNavigableMap

A ConcurrentMap supporting NavigableMap operations, and recursively so for its navigable sub-maps.

TransferQueue: 

A BlockingQueue in which producers may wait for consumers to receive elements.

Concurrent Collection's classes

Classes

Description

ArrayBlockingQueue

A blocking queue class based on bounded Java Array. Once instantiated, cannot be resized.

SynchronousQueue

 

A blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa.

LinkedBlockingQueue

An optionally-bounded blocking deque based on linked nodes.

LinkedBlockingDeque

An optionally-bounded blocking queue based on linked nodes.

LinkedTransferQueue

Implementation class of TransferQueue. An unbounded TransferQueue based on linked nodes.

PriorityBlockingQueue

An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations.

ConcurrentHashMap

A hash table supporting full concurrency of retrievals and high expected concurrency for updates.

ConcurrentSkipListMap

A scalable concurrent ConcurrentNavigableMap implementation.


 

Example:

Synchronized HashMap Implementation

import java.util.Collections;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class TestHashMap extends Thread {
private static Map<String, Integer> hm = Collections.synchronizedMap(new HashMap<String, Integer>());
public void run() {
hm.put("Four", 4);
}
public static void main(String[] args) {
hm.put("One", 1);
hm.put("Two", 2);
hm.put("Three", 3);
TestHashMap t1 = new TestHashMap();
t1.start();
for (Object o : hm.entrySet()) {
System.out.println(o);
}
System.out.println(hm);
}
}


ConcurrentHashMap Implementation:

import java.util.Collections;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class TestHashMap extends Thread {
private static ConcurrentHashMap<String, Integer> hm = new ConcurrentHashMap<String, Integer>();
public void run() {
hm.put("Four", 4);
}
public static void main(String[] args) {
hm.put("One", 1);
hm.put("Two", 2);
hm.put("Three", 3);
TestHashMap t1 = new TestHashMap();
t1.start();
for (Object o : hm.entrySet()) {
System.out.println(o);
}
System.out.println(hm);
}
}


Comments

Popular posts from this blog

Java Streams API - Process set of elements

Java 8 Vs Java 9