Incorrect lazy initialization of static field

utilizing sonar, or findbug, report above error message for this code.

public static void updateQuotation(String symbol, RawQuotation newValue) {

if (quotations == null) {
quotations = new HashMap();

quotations.put(symbol, newValue);

quotations is a static variable.

what findbug suggesting is actually about thread safety.

Incorrect lazy initialization and update of static field
This method contains an unsynchronized lazy initialization of a static field. After the field is set, the object stored into that location is further accessed. The setting of the field is visible to other threads as soon as it is set. If the futher accesses in the method that set the field serve to initialize the object, then you have a very serious multithreading bug, unless something else prevents any other thread from accessing the stored object until it is fully initialized.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s