See Singleton Pattern here...
package me.dhanoop.singleton; /** * * @author dhanoopbhaskar */ public class Singleton { private static class HelperHolder { public static Helper helper = new Helper(); } public static Helper getHelper() { return HelperHolder.helper; } public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { Singleton.getHelper(); } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); } }
Here as the helper object is static, an alternative is used - initialization-on-demand holder idiom. This is based on the fact that inner classes are not loaded until they are referenced.
0 Comments