Correct, if it did this instead: public class JaasSecurityManager { private CachePolicy domainCache;
private Subject updateCache(Principal principal) { synchronized( domainCache ) { domainCache = new CachePolicy(); // stuff } } } Another thread can enter the synchronized block because it will synchronize on a different object. I'm not sure it is even determinate which object it will synchronize on under the old (pre Java5) memory model? And probably only determinate on Java5 if you do private volatile CachePolicy domainCache; 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 -- xxxxxxxxxxxxxxxxxxxxxxxx Adrian Brock Chief Scientist JBoss Inc. xxxxxxxxxxxxxxxxxxxxxxxx ------------------------------------------------------- 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&kid=110944&bid=241720&dat=121642 _______________________________________________ JBoss-Development mailing list JBoss-Development@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-development