This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Need your help" Watch "Need your help" New topic
Author

Need your help

Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
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

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


ms<br />SCJP, SCJD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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: 1297
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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: 11404
    
  81

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: http://aspose.com/file-tools
 
subject: Need your help
 
Similar Threads
Implementin Unreferenced
NX: lock(Data data, int recNo) allowing multiple locks of one record!! why???
StackOverFlowError in Iterator !! Please Help !!
Record Locking Test
NX: About data consistent