Created
February 14, 2016 21:11
-
-
Save deyindra/772a6ecd032e23723627 to your computer and use it in GitHub Desktop.
Singleton Deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Singleton implements Serializable { | |
private static volatile Singleton object; | |
private Singleton() { | |
} | |
public static Singleton getInstance() { | |
if (object == null) { | |
synchronized (Singleton.class) { | |
if (object == null) { | |
object = new Singleton(); | |
} | |
} | |
} | |
return object; | |
} | |
//If Implement Serialization then it should return the same singleton object | |
protected Object readResolve() throws ObjectStreamException { | |
return Singleton.getInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
helpful and quite useful