The below code is receiving concurrent modificationexception when 2 thread access the same.
I would like to know whether
- Whether this exception can be avoided if we use concurrent Hashmap.
- If we use concurrent hashmap will there be any issue in a multithreaded environment.
or is there any other way to prevent this exception?
I donot intend use synchronzed as this code is used during polling. as one thread may have to wait for another to finish exceution.
The code is
HashMap<Integer, myModel> aServiceHash = new HashMap<Integer, myModel>();
HashMap<Integer, myModel> rServiceHash = new HashMap<Integer, myModel>();
for (myModel ser : serAccepted){
aServiceHash.put(service.getOriginalDate(), ser);
}
for (myModel ser : serRequested) {
if (aServiceHash.containsKey(service.getDate())) {
aServiceHash.put(serv.getDate(), serv);
}
else
rServiceHash.put(service.getDate(), ser);
}
http://www.journaldev.com/378/how-to-avoid-concurrentmodificationexception-when-using-an-iterator
Using JSF 2.1,JDK 7.1.