singleton with volatile in java

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

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


        
9条回答
  •  暖寄归人
    2020-12-31 06:43

    first of all, having a Singleton this way, you are essentially creating a global object which is a bad practice. I reckon you use Enums instead.

提交回复
热议问题