Tuesday, January 31, 2012

Java Singleton - The Basics

There are number of ways to implement singleton in Java.
The very basic:
public class SingletonClass {
 
 private static final SingletonClass singleton;
 
 public static getSingletonInstance() {
  if (singleton == null) {
   singleton = new SingletonClass();
  }
  return singleton;
 }
}
However, using this way is not good if this is in a multi-threaded environment. The method should use the "synchronized" keyword to make it thread safe.
public static synchronized getSingletonInstance() {
 if (singleton == null) {
  singleton = new SingletonClass();
 }
 return singleton;
}
or
public static getSingletonInstance() {
 synchronized(SingletonClass.class) {
  if (singleton == null) {
   singleton = new SingletonClass();
  }
 }
 return singleton;
}
"synchronized" can be slowed because other threads will have to wait for it. It might not be a problem if only a few threads will be calling it at the same time. However, if there are a lot of threads trying to get the instance very frequently, then this should be a better approach:
public class SingletonClass {
 
 private static final SingletonClass singleton = new SingletonClass();
 public static getSingletonInstance() {
  return singleton;
 }
}
The shared instance is initialized in the static block so that there is no check in the method nor other threads need to wait for it. The drawback of this is that there is no lazy-load. The shared instance will be initialized even if the method is never called.
The thread-safe and lazy-load way from Bill Pugh:
public class SingletonClass {

        private SingletonClass() { 
  }
 
        private static class SingletonClassInstance { 
                public static final SingletonClassInstance instance = new SingletonClassInstance();
        }
 
        public static SingletonClass getInstance() {
                return SingletonClassInstance.instance;
        }
}
Thread-safe without the "synchronized" keyword and the instance is initialized on the first call. However, so far all of the above implementation needs to consider the serializable objects. If the fields of the singleton class are non-transient, attack can be done thru serializing and deserializing the SingletonClass.

Using enum to implement singleton base on Effective Java:
public enum SingletonClass {
 INSTANCE;
 
 public void doSomething() {
 }
}
Very clear implementation, guarantee against multiple instantiation, lazy-load, and no problem with the serializable objects like all of the above implementations. This should be so far the best way to implement Java singleton.

3 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    ReplyDelete
  3. Nice blog & thanks for the share.

    Checkout http://skartecedu.in for the best digital marketing training in Chennai.

    ReplyDelete