Part 1
Interview questions on Concurrent Collections in java for experienced covered in this post:
What is Concurrency? What is the need of Concurrency?
-
Concurrency is the ability to run multiple programs at a
time.
-
For concurrency threads are responsible.
-
Concurrency package provides more flexible classes than
synchronized methods or blocks.
-
Concurrency belongs to java.util.Concurrent package.
-
Needs of Concurrency are as follows.
1.
Traditional collection object ArrayList, LinkedList, HashSet
and Hashmap access by multiple threads so data inconsistency problem may occur
hence, these are not thread-safe. But, Concurrency classes are
thread-safe.
2.
Already existing thread-safe collection Vector, Stack,
SynchronizedMap, SynchronizedSet are not good performance-wise. But,
Concurrent package classes are very good performance-wise.
3.
Only one thread can execute thread-safe objects so other
threads must wait until the first thread completes its task so performance
goes down. Concurrent classes overcome these problems.
4.
Another big problem is, when one thread is iterating collection
objects, the other thread can not modify the same collection object. When the
first tread is iterating the collection object at the same time another thread
is trying to modify the collection object then we will get
ConcurrentModificationException.
What is the difference between Traditional Collection and Concurrent
Collection?
|
Traditional Collection |
Concurrent Collection |
1. |
All traditional collections are not thread-safe. |
All Concurrent collections are thread-safe. |
2. |
Thread-safe traditional collections performance is low as compared to
Concurrent collections. |
Concurrent Collections performance is high as compared to the
traditional collection. |
3. |
While one thread is executing the collection object no other thread is
allowed to access the same collection object. |
In Concurrent collection, while one thread is executing a collection
object another thread is allowed to access the same collection
object. |
4. |
ConcurrentModificationException can be thrown when multiple threads
execute the same resources. |
ConcurrentModificationException cannot occur when multiple threads
executing the same resources. |
5. |
Traditional Thread-safe classes are follows.
1.
Vector
2.
Stack
3.
Hashtable
4.
Properties |
Concurrent collections are as follows.
1.
ConcurrentHashMap
2.
CopyOnWriteArrayList
3.
CopyOnWriteArraySet |
What is ConcurrentHashMap? What are the Characteristics of
ConcurrentHashMap?
-
ConcurrentHashMap is the concurrent class with key and value
pairs belonging to java.util.Concurrent package.
-
ConcurrentHashMap is a concurrent version of HashMap. We can
say it is a thread-safe HashMap.
-
Characteristics of ConcurrentHashMap are as follows.
1.
Characteristics of ConcurrentHashMap are as follows.
2.
The underlying data structure is Hashtable.
3.
Null is not allowed either key or value.
4.
ConcurrentHashMap allows any number of reading operations at a
time and only 16 write operations at a time when the concurrency level is
16.
5.
Instead of getting a whole map object lock concurrency divides
map objects into 16 parts because the default size of ConcurrentHashMap is
16.
6.
To perform read operation would not require any type of lock
but for write operation thread must get Bucket level lock or Segment level
lock.
7.
ConcurrentHashMap allows concurrent read and thread-safe write
operations.
8.
While one thread performs read operations other 16 threads can
perform write operations at a time.
9.
ConcurrentHashMap never throw
ConcurrentModificationException.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapDemo {
public
static
void
main(String[] args) {
ConcurrentHashMap<Integer, String> map
= new
ConcurrentHashMap<Integer, String>();
map.put(101, "Yogesh");
map.put(102, "Arpita");
map.putIfAbsent(103, "Shweta");
System.out.println(map);
map.putIfAbsent(102, "Shruti");
map.putIfAbsent(104, "Shivani");
System.out.println(map);
map.replace(104, "Prajakta");
System.out.println(map);
map.remove(101);
System.out.println(map);
}
}
Output:
{101=Yogesh, 102=Arpita, 103=Shweta}
{101=Yogesh, 102=Arpita, 103=Shweta, 104=Shivani}
{101=Yogesh, 102=Arpita, 103=Shweta, 104=Prajakta}
{102=Arpita, 103=Shweta, 104=Prajakta}
|
What do you mean by Concurrency Level?
- Concurrency divided map objects into 16 parts that smaller parts are known as Concurrency level.
What is the default concurrency level of ConcurrentHashMap?
-
Default concurrency level is 16 because the default size of the
HashMap is 16.
-
Java interview questions and answers all MNC - Click here
-
Basic core java interview questions and answers for freshers
- Click here
-
Core java interview questions for 3 years experience - Click here
-
Core java interview questions and answers for 3-5 years exp
- Click here
-
Core java interview questions and Answers for 5 - 7 Years exp
- Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced
- Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on
some support projects and then I moved to java projects.
I had worked with many multi-national companies like
R-Systems, Tata Consultancy Services, Cybage
Softwares. Fortunately, TCS and Cybage has
given me an opportunity to take interviews for
experienced candidates. I have conducted more than 1000
interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments