ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

设计模式:单例模式(Singleton Pattern、懒汉)

设计模式:单例模式(Singleton Pattern、懒汉) /** * 单例模式。 * author Bright Lee */ public class Singleton { private static volatile Singleton instance; private Singleton() { System.out.println(构造方法被调用了当前时间戳是 System.currentTimeMillis()); } public static Singleton getInstance() { if (instance null) { synchronized(Singleton.class) { if (instance null) { instance new LazySingleton(); } } } return instance; } public static void main(String[] args) { // 并发调用100遍构造方法只会被调用一次 for (int i 0; i 100; i) { new Thread() { public void run() { Singleton.getInstance(); } }.start(); } } }青蛙客服系统https://download.csdn.net/download/look4liming/93326820
返回列表