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

Rémi Forax forax at univ-mlv.fr
Sat Mar 22 09:15:06 EDT 2008


Jason Mehrens a écrit :
> 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.
This case is cover by a more general rule: don't synchronized on 
internable object
like Class, String, wrappers (Integer, etc.).
>  
> 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++;
>     }
>   }
> }
>
>  
> Jason
Rémi


More information about the Concurrency-interest mailing list