You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by suhas <su...@techmas.hcltech.com> on 2000/07/01 11:47:37 UTC

Fast Hashmap doubt .

I was going through put ( ) and putAll ( ) methods in Fast HashMap code in the struts . But did not get why we need to clone the HashMap and add a entry to that cloned map then move that cloned map to original HashMap . In multi- threaded enviornment anyway we synchronized the code that accesses the HashMap in the put method .. ?

    public Object put(Object key, Object value) {

        if (fast) {
            synchronized (this) {                                            // IF U synchronize the code like this then easily only one thread can manipulate the HashMap

                HashMap temp = (HashMap) map.clone();        // What this do then ?
                Object result = temp.put(key, value);                //
                map = temp;                                                   //
                return (result);
            }
        } else {
            synchronized (map) {
                return (map.put(key, value));
            }
        }

    }






Re: Fast Hashmap doubt .

Posted by Calvin Yu <ca...@mindspring.com>.
If you look at FashHashMap.get(Object key), you'll notice that the call
to map.get() is not synchronized.  If FashHashMap.put() isn't
implemented like this, you'll run into a race condition when one thread
calls map.put() and another calls map.get().
 
Calvin

----- Original Message ----- 
From: suhas <ma...@techmas.hcltech.com>  
To: struts-user@jakarta.apache.org
<ma...@jakarta.apache.org>  
Sent: Saturday, July 01, 2000 5:47 AM
Subject: Fast Hashmap doubt .


I was going through put ( ) and putAll ( ) methods in Fast HashMap code
in the struts . But did not get why we need to clone the HashMap and add
a entry to that cloned map then move that cloned map to original HashMap
. In multi- threaded enviornment anyway we synchronized the code that
accesses the HashMap in the put method .. ?
 
    public Object put(Object key, Object value) {
 
        if (fast) {
            synchronized (this) {
// IF U synchronize the code like this then easily only one thread can
manipulate the HashMap

                HashMap temp = (HashMap) map.clone();        // What
this do then ?
                Object result = temp.put(key, value);                //
                map = temp;
//
                return (result);
            }
        } else {
            synchronized (map) {
                return (map.put(key, value));
            }
        }
 
    }