One must synchonize the put and get operation on the map itself
in order to protect its internal consistency.

  Map map = Collections.synchronizedMap(new HashMap());
  ...
  map.put(key, value);
  ...
  value = map.get(key);

is just easier to read than

  Map map = new HashMap();
  ...
  synchronized(map) { map.put(key, value); }
  ...
  synchronized(map) { value = map.get(key); }

although with just one get and one put the significance may be argued.

You don't want to replace the outer synchronized(serverSession) by 
synchronized(sessions) because that blocks all threads without being
necessary (although the effect will be immeasurable).

You must have synchronized(serverSession) (or block all threads) to 
avoid calling sessions.put twice for the same serverSession.

Cheers, Alfred.


> -----Original Message-----
> From: Ralph Goers [mailto:[EMAIL PROTECTED]
> Sent: Freitag, 13. Mai 2005 01:51
> To: [email protected]
> Subject: Re: svn commit: r169856 -
> /cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/envir
> onment/htt
> p/HttpRequest.java
> 
> 
> Why is "sessions" a synchronized map if you are only 
> accessing it in a 
> block synchronized on the session.  I would much prefer that you
> a) not use a synchronized map
> b) synchronize on the map instead of the session.
> 
> Is there a reason that this wouldn't work?
> 
> Ralph
> 
> [EMAIL PROTECTED] wrote:
> 
> >Author: joerg
> >Date: Thu May 12 10:50:20 2005
> >New Revision: 169856
> >
> >URL: http://svn.apache.org/viewcvs?rev=169856&view=rev
> >Log:
> >fixed weak referencing (thanks to Alfred Nathaniel)
> >
> >Modified:
> >    
> cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/enviro
> nment/http/HttpRequest.java
> >
> >Modified: 
> cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/enviro
> nment/http/HttpRequest.java
> >URL: 
> http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src
/java/org/apache/cocoon/environment/http/HttpRequest.java?rev=169856&r1=169855&r2=169856&view=diff
>==============================================================================
>--- 
>cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/environment/http/HttpRequest.java
> (original)
>+++ 
>cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/environment/http/HttpRequest.java
> Thu May 12 10:50:20 2005
>@@ -230,11 +230,11 @@
>             // synch on server session assures only one wrapper per session 
>             synchronized (serverSession) {
>                 // retrieve existing wrapper
>-                session = (HttpSession)sessions.get(serverSession);
>+                session = 
>(HttpSession)((WeakReference)sessions.get(serverSession)).get();
>                 if (session == null) {
>                     // create new wrapper
>                     session = new HttpSession(serverSession);
>-                    sessions.put(serverSession, session);
>+                    sessions.put(serverSession, new WeakReference(session));
>                 }
>             }
>         } else {
>
>
>  
>
 
 
This message is for the named person's use only. It may contain confidential, 
proprietary or legally privileged information. No confidentiality or privilege 
is waived or lost by any mistransmission. If you receive this message in error, 
please notify the sender urgently and then immediately delete the message and 
any copies of it from your system. Please also immediately destroy any 
hardcopies of the message. You must not, directly or indirectly, use, disclose, 
distribute, print, or copy any part of this message if you are not the intended 
recipient. The senderâs company reserves the right to monitor all e-mail 
communications through their networks. Any views expressed in this message are 
those of the individual sender, except where the message states otherwise and 
the sender is authorised to state them to be the views of the senderâs 
company.

Reply via email to