Concurrent Collection Interview Questions

CONCURRENT COLLECTION INTERVIEW QUESTIONS

What is the need of concurrent collection ?

Main reasons for concurrent collections are

  • Traditional collections are not thread safe. only few classes like Vector, HashTable are thread safe.
  • Collections provide some methods like synchronizedList, synchronizedMap, synchronizedSet those provide thread safety but problem is they capture lock on complete collection even for reading that decreases performance.
  • In traditional collection if one thread iterates and other tries to modify structural change then ConcurrentModificationException is thrown.

Give few concurrent collections classes ?

  • ConcurrentHashMap
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet

In which package all concurrent classes are kept ?

java.util.concurrent

What is the difference between remove(Object key) and remove(Object key, Object value) in ConcurrentMap ?

remove(Object key) removes Object from Map if that key exist in Map while remove(Object key, Object value) removes Object from Map if that key and its corresponding value match.

What is the default capacity of ConcurrentHashMap ?

16

Why performance of ConcurrentHashMap is better than HashTable and synchronizedMap ?

In HashTable and synchronizedMap lock is acquired on complete collection so only single thread can capture lock at a time while in ConcurrentHashMap lock is acquired on bucket level so at a time multiple thread can capture lock on different different bucket.

What is concurrency level in ConcurrentHashMap ?

concurrency level defines how many thread are allowed to get lock.As by default ConcurrentHashMap has 16 bucket and concurrency level also has 16 value.It means 16 thread can take lock at a time each per bucket.We can change concurrency level value as well for example if we do 8 then all buckets counts are divided by 8 and resulted number of bucket will keep single lock.

Give one example of ConcurrentHashMap.


package com.example.preparationforinterview;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample {

	public static void main(String args[]) {

		ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap<>();
		concurrentHashMap.put("A", 1);
		concurrentHashMap.put("B", 2);
		concurrentHashMap.put("C", 3);
		
		concurrentHashMap.putIfAbsent("D", 4);
		concurrentHashMap.putIfAbsent("A", 5);
		
		concurrentHashMap.remove("A", 5);
		
		concurrentHashMap.replace("A", 1, 6);
		
		System.out.println(concurrentHashMap);
	}
}

output is


{A=6, B=2, C=3, D=4}

What is the difference between HashMap and ConcurrentHashMap ?

HashMapConcurrentHashMap
HashMap is thread unsafeConcurrentHashMap is thread safe
Performance is highPerformance is low
HashMap can contain null key and value bothConcurrentHashMap can not contain null as a key/value pair.
It is fail fast, if one thread is iterating map and other thread tries to do structural change then we get CocurrentMofificationException. ConcurrentHashMap is fail safe , if one thread is iterating map then other can update same map.

What is CopyOnWriteArrayList ?

It is thread safe version of List and are kept in concurrent package.For every write operation a separate clone copy is created and JVM will synchronize the clone and actual copy.

What is the difference between ArrayList and CopyOnWriteArrayList ?

ArrayListCopyOnWriteArrayList
ArrayList is thread unsafeCopyOnWriteArrayList is thread safe
Performance is highPerformance is low
It is fail fast, if one thread is iterating ArrayList and other thread tries to do structural change then we get CocurrentMofificationException. CopyOnWriteArrayList is fail safe , if one thread is iterating List then other tries to update List then new clone will be created and later JVM will synchronize the clone and actual copy.
Iterator can remove elementif Iterator tries to remove element then UnsupportedOperationException exception is thrown.

What is IdentityHashMap ?

In HashMap when comparing keys ,it perform object equality.It means two keys are same if their content is also same while in IdentityHashMap keys are same when their reference is same for example k1==k2.

Does equals() and hashCode() method are called in IdentityHashMap ?

No, they are not called when put, get methods are called because its call its own equals() and hashCode() for reference equality.

What is WeakHashMap ?

It is HashTable based implementation.in WeakHashMap entry is removed by garbage collector from map if it was not used since long time.

Which sorting is used by Collections ?

If elements are less than 7 then Insertion Sort is used otherwise Merge Sort.

Can multiple threads read from ConcurrentHashMap at the same time ?

Yes, during read operation lock is not required.

Can read and write happen at same time from same segment ?

No if its happening from same segment then lock is acquired during operation.

Why null is not allowed in ConcurrentHashMap ?

As we know that in ConcurrentHashMap multiple thread is allowed to operate and for reading purpose we don't require lock.So just consider following example


Line 1: if (concurrentHashMap.containsKey(k)) {     
Line 2:      return concurrentHashMap.get(k);  
Line 3: } else {
Line 4:      throw new KeyNotPresentException();
Line 5: }

In this example if thread one checks at Line 1 that key is there and goes to second line but its possible after reading first line by thread1, second thread deletes that key from ConcurrentHashMap so when thread1 will execute line2 then he will get null but he will not get idea why null is returned because in two cases null is returned either key is null or value is null thats why to remove ambiguity nature null is not allowed.In HashMap we don't get this ambiguity because there is no concurrent modification allowed.




Visit Others Links

Interview Questions Top Elasticsearch Interview Question
Top Kubernetes Interview Question
Top HTTP Interview Question
Top PouchDB Interview Question
Top Pivotal Cloud Foundry (pcf) Interview Question
Top Apache Camel Interview Question
Top Docker Interview Question
Top Gradle Interview Question
Top Generics Interview Questions In Java
Java 9 Interview Questions
Top Maven Interview Question
Top RabbitMQ Interview Question
Top Spring Batch Interview Question
Top Spring Boot Interview Question
Top Tika Interview Question
Top Kotlin Interview Question
Top OpenShift Interview Question
Top Polymer Interview Question
Top WebGL Interview Question
Top Continuous Integration Interview Question
Top Spring Cloud Interview Question
Top JOGL Interview Question
Top JFreeChart Interview Question
Java MultiThreading Interview Question
Top Spring ROO Interview Question
Top Seaborn Interview Question
Top TestRail Interview Question
Top LolCode Interview Question
Top Spring Transaction Management Interview Question
Top DynamoDB Interview Question
Top PySpark Interview Question
Top SaltStack Interview Question
Top Spring AOP Interview Question
© 2019 PreparationForInterview.com