The problem in the JaasSecurityManager class example below is that the
domainCache variable itself needs to be protected with synchronization
since its non-final (for all domainCache read/write operations.)

It is not fine as one thread could be performing updateCache(Principal),
meanwhile, another thread assigns a new value to domainCache.  The
thread performing updateCache, wouldn't have a consistent memory view of
the new domainCache and also violates atomicity. 

On Tue, 2006-04-11 at 23:51 -0500, Scott M Stark wrote:
> I'm trying to understand the full implication of the intellij
> synchronization on non-final fields warning, which has this description:
> 
> This inspection reports instances of synchronized statements where the
> lock expression is a non-final field. Such statements are unlikely to
> have useful semantics, as different threads may be locking on different
> objects even when operating on the same object.
> 
> An example usage is the following where a multiple operations on the
> domainCache variable is done in a synchronized block so that the
> remove/insert are atomic:
> 
> public class JaasSecurityManager
> { 
>    private CachePolicy domainCache;
> 
>    private Subject updateCache(Principal principal)
>    {
>       synchronized( domainCache )
>       {
>          if( domainCache.peek(principal) != null )
>             domainCache.remove(principal);
>          domainCache.insert(principal, info);
>       }
>    }
> }
> 
> In going over the current memory model docs:
> http://www.cs.umd.edu/~pugh/java/memoryModel/
> http://gee.cs.oswego.edu/dl/jmm/cookbook.html
> http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf
> 
> The only context I can see where this warning applies is if the
> domainCache variable is being changed. In that case two threads may
> actually be synchronizing/working on different objects, but that is fine
> in this case as the atomic block of ops only applies to the domainCache
> object. If other state was being referenced I can see a problem, but not
> for the illustrated usage.
> 
> Am I missing something here?
> 
> xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Scott Stark
> VP Architecture & Technology
> JBoss Inc.
> xxxxxxxxxxxxxxxxxxxxxxxxxxxx 
>  
> 
> 
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting language
> that extends applications into web and mobile media. Attend the live webcast
> and join the prime developer group breaking into this new coding territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
> _______________________________________________
> JBoss-Development mailing list
> JBoss-Development@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jboss-development



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JBoss-Development mailing list
JBoss-Development@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to