Part 4
Interview questions on Concurrent Collections in java for experienced covered in this post:
Will it throw ConcurrentModificationException while iterating ConcurrentHashMap? If No Why?
-
While iterating ConcurrentHashMap it won’t throw
ConcurrentModificationException.
-
Thread will get a bucket level or Segment level lock instead
of a whole object lock.
-
Multiple threads are allowed to perform a thread-safe
operation at a time.
-
Read operation can perform without lock and the write
operation can perform by getting bucket level or Segment level lock.
-
When one thread is performing a read operation other threads
are allowed to perform a write operation on the same object still you won’t
get ConcurrentModificationException.
-
Please refer to the below example. Please cross verify with
the above example as well.
package simplifiedjava.crackedInterview;
import
java.util.Iterator;
import
java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentModificationExceptionDemo {
public
static
void
main(String[] args) {
ConcurrentHashMap<Integer,String> m
= new
ConcurrentHashMap<Integer,String>();
m.put(101,"Shweta"); m.put(102,"Shruti");
Iterator itr
= m.entrySet().iterator();
while(itr.hasNext()) {
m.put(103, "Anjali");
Map.Entry<Integer, String> pair
= (Map.Entry<Integer, String>)itr.next();
System.out.println("Key = "+ pair.getKey()+ "\t Value = "+ pair.getValue());
}
}
}
Output:
Key = 101
Value = Shweta
Key = 102
Value = Shruti
Key = 103 Value = Anjali |
What will happen while iterating any collection object including map trying to perform some operations like read or write on the object?
-
It will throw ConcurrentModificationException.
-
For more details, you may refer last 5 questions.
Iterator of Concurrent Collection object is Fail Safe or Fail Fast?
-
Iterators of Concurrent Collections are Fail-Safe.
-
Fail-safe means iterator doesn’t fail if you are trying to
modify the element at the time of iteration.
-
The Iterator will not fail while updating Concurrent
Collection objects at the time of iteration.
Can I perform remove operation while iterating CopyOnWriteArrayList object?
-
We cannot perform the remove operation while iterating the
CopyOnWriteArraylist object.
-
It is possible for List implementations.
What will happen if I am trying to perform remove operation while iterating CopyOnWriteArrayList?
-
If we are trying to perform a remove operation while
iterating CopyOnWriteArrayList, it will throw
UnSupportedOperationException.
-
Please refer to the bleow example.
package simplifiedjava.crackedInterview;
import
java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListDemo {
public
static
void
main(String[] args) {
CopyOnWriteArrayList<Integer> list
= new
CopyOnWriteArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(40); list.add(50);
Iterator itr
= list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
itr.remove();
}
}
}
Output:
10
Exception in thread "main" java.lang.UnsupportedOperationException
at
java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(Unknown
Source)
at
simplifiedjava.crackedInterview.CopyOnWriteArrayListDemo.main(CopyOnWriteArrayListDemo.java:19) |
-
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