Partially constructed objects in a thread-safe singleton

In a multi-threaded environment, how can a stream see a “partially constructed object”? I realized that it is not thread safe, as multiple threads can create multiple instances.

class LazyInit
{  private static Resource resource = null;

    public static getInstance()
    {  if (resource == null) { resource = new Resource();  }
       return instance;
    }
}
+5
source share
1 answer

Because of the record because of the order.

, , . Java , , , , , , .
. Java.

, , . , , if , . , CPU, null-null, . , .
, , Resource , getInstance. , singleton.

+7

All Articles