[concurrency-interest] New bug pattern, way to common

Dhanji R. Prasanna dhanji at gmail.com
Fri Mar 21 18:53:38 EDT 2008


On Sat, Mar 22, 2008 at 4:01 AM, Jason Mehrens <jason_mehrens at hotmail.com>
wrote:

>  Another broken pattern that findbugs (1.1.3) doesn't catch is using
> getClass() and synchronized.  Since getClass() doesn't always return the
> Class object that defines the static field (because of a sub class) it is a
> case of synchronized on different objects.  This may not be common but it is
> broken.
>
> The following is an example, the original was from "yet another broken
> double checked locking attempt":
>
> public class NonFinalClassShouldUseClassLiteral {
>   private static int count;
>   public NonFinalClassShouldUseClassLiteral() {
>     synchronized (getClass()) {
>       count++;
>     }
>   }
> }
>
>
>

Good catch! Probably a very marginal case?

I think an inspection for instance field = Logger.getLogger(getClass()) may
also be worthwhile (though OT).

Dhanji.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080322/8f506557/attachment.html 


More information about the Concurrency-interest mailing list