Synopsis: | Singleton is not thread safe |
Language: | Java |
Severity Level: | 1 |
Category: | Design |
Description: |
Non-thread safe singletons can result in bad state changes. Eliminate
static singletons if possible by instantiating the object directly. Static
singletons are usually not needed as only a single instance exists anyway.
Other possible fixes are to synchronize the entire method or to use an
initialize-on-demand holder class (do not use the double-check idiom).
See Effective Java, item 48.private static Foo foo = null; //multiple simultaneous callers may see partially initialized objects public static Foo getFoo() { if (foo==null) foo = new Foo(); return foo; } |