Two Laptop Bag
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Need your help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Need your help" Watch "Need your help" New topic

Need your help

Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
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); // line 298
lockRecords.remove(new Integer(record));

It throws
at java.util.HashMap$HashIterator.nextEntry(
at java.util.HashMap$
at suncertify.server.RemoteAccess.unreferenced(
at sun.rmi.transport.Target$
Could someone tell me why and how to handle it?
Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
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<br />SCJP, SCJD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

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

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1304
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?

BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

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.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Use the remove method of the Iterator, not the remove method of "lockRecords", when removing elements.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

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
I agree. Here's the link:
subject: Need your help
It's not a secret anymore!