• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Need your help

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic