Re: RMI Serializing Error

Jason Sicotte <[email protected]>
Newsgroups gmane.comp.java.sun.rmi
Message-ID <LISTSERV%[email protected]>
The code I posted was thread safe as far as I can tell.  Section 8.13 of the
JVM specification in part states:

"If a variable is ever to be assigned by one thread and used or assigned by
another, then all accesses to that variable should be enclosed in
synchronized methods or synchronized statements"

Thus access to a shared HashMap should be synchronized.  In addition to
this, the J2SE 1.4 API for HashMap advises that one use the Collections
synchronizedMap() method to obtain a synchronized wrapper for a HashMap
instance.

In the example I posted, the HashMap is wrapped by the Collections wrapper
mentioned above and is modified inside synchronized blocks:

    synchronized(this.serverMap){
        for(int i=0; i<10000 ; i++){
            this.serverMap.put(new Integer(i),new Integer(i));
        }
        try{Thread.sleep(1000);}catch(InterruptedException e){}
    }
    synchronized(this.serverMap){
        this.serverMap.clear();
    }

Also, when the get() method is called, access to the HashMap is synchronized
there as well:

    public Map getMap() throws RemoteException {
        synchronized (this.serverMap){
            return this.serverMap;
        }
    }

The RMI thread gets a reference to the HashMap safely. But the problem is
that it calls HashMap.writeObject() which is not safe and the Iteration fails.

The only fix I have found so far is to return a clone of the map to RMI so
that the iteration will not fail.  Making a copy of every object for each
get() call seems slow and clumsy.

Seeming as you, (and I am certain others on the list) are more knowledgeable
than I on the subject of threads, let me sate my question as this:  How can
I modify the application outlined in

http://archives.java.sun.com/cgi-bin/wa?A2=ind0412&L=rmi-users&F=&S=&P=3197

to lock access to a HashMap reference from an external thread after that
thread has already obtained a reference?  This must be done without
modification to the calling thread.


>> I would like to ask the RMI community what possible solutions are available
>> to  the an issue I am experiencing.  From what I can tell, RMI has an
>> inability to reliably pass a HashMap to a client.  However, the problem
>> only occurs when said HashMap has been changed by another thread on the
>> server.

>It is not RMI's responsibility to make your application thread safe. You will
>have massive amount of other similar concerns of modifications of objects
>during serialization.

>I advice you that you make your application thread-safe, since the effort is
>required from you, not RMI.
>
>
>Cheers
>Niclas

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff RMI-USERS".  For general help, send email to
[email protected] and include in the body of the message "help".

For a list of frequently asked RMI questions please refer to:
http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html

To view past RMI-USERS postings, please see:
http://archives.java.sun.com/archives/rmi-users.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.