1. The A hungry man is created when the JVM is started in a singleton class, and the lazy type is created when the first instance of a singleton class is accessed, that is, the A hungry man is considered preload and the lazy load is deferred;
Sample code:
public class Eagersingleton {private static final Eagersingleton instance = new Eagersingleton (); Private Eagersingleton ( {System.out.println ("initialized..");} public static Eagersingleton getinstance () {return instance;} public void pri NT () {System.out.println ("This was an eager singleton.");}
2, a hungry man type of singleton class instances are created when the JVM is started, so do not consider multithreading problems, the lazy type needs to be synchronized (synchronized);
Sample code:
public class Lazysingleton {private static Lazysingleton instance = null; private Lazysingleton () {System.out.println ("I nitialized.. ");} public static synchronized Lazysingleton getinstance () {if (instance = = null) {instance = new Lazysingleton (); Instance public void print () {System.out.println (' This is a lazy singleton. ');}