简体   繁体   English

Singleton with Enum vs Singleton with double-checked locking

[英]Singleton with Enum vs Singleton with double-checked locking

I am wondering which one is better in practice in a multithreaded environment. 我想知道在多线程环境中哪一个更好。 I understand Singleton as Enum type creates an instance when the class is loaded. 我理解Singleton作为Enum类型在加载类时创建一个实例。 Other than that I don't see anything else significant. 除此之外我没有看到任何其他重要的东西。 Is there any pros and cons ? 有利有弊吗?

Singleton as Enum type: Singleton as Enum类型:

 public enum Singleton {
      INSTANCE;
      public void doSomething(){ ... }
 }

Singleton with double-checked locking : Singleton double-checked locking

 public class Singleton{
      private volatile static Singleton instance;

      private Singleton(){}
      public static Singleton getInstance(){
           if(instance == null){
                 synchronized(Singleton.class){
                       if(instance == null){
                           instance = new Singleton();
                       }
                 }
           }
           return instance;
      }

 }

Often in multi-threaded applications, simpler, easier to understand code is more likley to work. 通常在多线程应用程序中,更简单,更易于理解的代码更有可能工作。

To my mind the first example is significantly simpler than the second and that is what matters. 在我看来,第一个例子比第二显著简单,这是最重要的。

The main pro of using an enum is its much simpler and the more complicated example is not justified. 使用enum的主要原因是它更简单,更复杂的例子是不合理的。 The double locking example allows you to change the singleton in interesting way for unit tests, but I believe what this gives you can be solved another way. 双锁定示例允许您以有趣的方式更改单例以进行单元测试,但我相信这可以通过另一种方式解决。

There are more problems with Singletons, than just a correct implementation. 单身人士遇到的问题多于正确的实施问题。

People often use Singletons, so they don't have to pass Objects around, where they should or they would have to pass Objects across multiple Methods. 人们经常使用单身人士,所以他们不必传递对象,他们应该在哪里,或者他们必须跨多个方法传递对象。

There are quite a few examples for instantiating a jdbc connection with a Singleton. 有一些例子用于实例化与Singleton的jdbc连接。

In the methods where you need this connection, you can easily access it, because its a Singleton. 在需要此连接的方法中,您可以轻松访问它,因为它是一个Singleton。

public enum DBConnection {
  INSTANCE;
  private Connection connection;
  public Connection getConnection(){
    if(connection == null){
      instantiateConnection();
    }
    return connection;
  }
}

and accessing it by 并通过访问它

DBConnection.INSTANCE.getConnection();

But often it's better to do it with Dependency Injection and a pleasent framework. 但通常最好使用依赖注入和一个非常好的框架来实现它。 GUICE for example. GUICE例如。

In your code you would not make a call on DBConnection.INSTANCE and than on getConnection(), you're Class would have a field DBConnection annotated with @Inject. 在您的代码中,您不会在DBConnection.INSTANCE上进行调用,而在getConnection()上,您将使用@Inject注释一个字段DBConnection。 And you just use it. 而你只是使用它。

@Inject
private DBConnection dBConnection;

And the Class would be just annotated with @Singleton to be a Singleton, and the framework would assure it's a singleton. 这个类只是用@Singleton注释为Singleton,框架会保证它是一个单例。

And if you have different environments like, test and production you could let the framework inject different Objects for each environment. 如果您有不同的环境,例如测试和生产,您可以让框架为每个环境注入不同的对象。

Using enum for creating a singleton have gained popularity for being simple and quick way to do it. 使用enum创建singleton已经因为简单快捷的方式而受到欢迎

Moreover you have compared here with Double check locking, but then there are other ways too, without using enum. 此外,你在这里比较了双重检查锁定, 但是还有其他方法,不使用枚举。

eg: 例如:

public class Test{

      private static Test uniqueInstance = new Test();

      private Test(){}

      public static Test getInstance(){


             return uniqueInstance;     
         }

 }

But i think enum makes quite a straight forward approach with a cleaner self explained code for singleton . 但我认为enum 使用一个更清晰的自我解释 singleton 代码 singleton一个直截了当的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM