• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Need your help  RSS feed

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my unreferenced(),the lockRecords keep a set of locked records in each remote connection object


public void unreferenced() {
System.out.println("Unreferenced called");
// the LockManager and lockRecords free all the locks and keys
Iterator iter = lockRecords.iterator();
while (iter.hasNext()) {
int record = ((Integer) iter.next()).intValue(); // line 298
unlock(record);
lockRecords.remove(new Integer(record));
}
}


It throws
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:762)
at java.util.HashMap$KeyIterator.next(HashMap.java:798)
at suncertify.server.RemoteAccess.unreferenced(RemoteAccess.java:298)
at sun.rmi.transport.Target$1.run(Target.java:321)
at java.lang.Thread.run(Thread.java:536)
Could someone tell me why and how to handle it?
Thanks!
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not positive that my solution will do it, but since HashMap is not thread safe, you need to synchronize the method or the code block. That way things will single-thread through and you should be ok.
ms
 
author and jackaroo
Posts: 12199
280
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ray

Could someone tell me why


The exception you are getting is ConcurrentModificationException. Concurrent meaning "at the same time". The JVM is complaining that someone is modifying data at the same time that it is trying to work with it.
So you have an iterator stepping over the lockRecords collection. But at the same time, something is modifying them: your call to lockRecords.remove().
Does this explain why you are getting the exception?

and how to handle it?


Instead of iterating over your collection, perhaps you could make an array of the elements of that collection. Then you can walk through the array, removing items from the collection safely.
Alternatively, since this collection seems specific to this instance of the Remote object, you could just unlock all the records in the iterator, then once complete, just empty the collection in one hit. There doesnt seem to be a need to remove the individual records during the iteration.
Regards, Andrew
 
Ranch Hand
Posts: 1323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just use a Hashtable, because it is synchronised like Vector is.
whats the difference between Hashmap and Hashtable? I found ppl in here more likely to use Hashmap instead why?
 
ranger
Posts: 17344
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Billy, the HashTable and Vector collections are Synchronized, the others are not.
They are also considered older collection versions and SUN has made better versions of them.
I know that is a quick and dirty way to explain why we like HashMap or ArrayList better.
Mark
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the remove method of the Iterator, not the remove method of "lockRecords", when removing elements.
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron Use the remove method of the Iterator, not the remove method of "lockRecords", when removing elements.


Much better (unless you want to just remove the entire lockRecords at the end).
Regards, Andrew
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!