singleton with volatile in java

前端 未结 9 1003
暖寄归人
暖寄归人 2020-12-31 06:23
class MyClass
{
      private static volatile Resource resource;

      public static Resource getInstance()
      {
            if(resource == null)
                        


        
9条回答
  •  时光说笑
    2020-12-31 06:50

    I think, you should use syncronized keyword before getInstance definition.

    For better performance you can use Double-checked locking pattern:

    • Double-checked locking and the Singleton pattern on IBM
    • Double-checked locking on Wiki

提交回复
热议问题