This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need your help

 
Ray Cheeny
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!
 
Mike Southgate
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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11865
194
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
 
Billy Tsai
Ranch Hand
Posts: 1304
  • 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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
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
 
Ron Newman
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
Marshal Commander
Pie
Posts: 11865
194
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic