package day21_reflact; //懒汉式单例模式 public class javaSingleton { public static void main(String[] args) { for (int i = 0; i < 3; i++) { new Thread(()->{//Lambda表达式 Singleton.getInstance().print(); },"单例消费端"+i) .start(); //线程启动 } } } class Singleton{ private static Singleton instance = null ; private Singleton (){ //创建线程进行查看 System.out.println("【"+Thread.currentThread().getName()+"】*******实例化instance对象********"); } public static Singleton getInstance () { if(instance == null){ instance = new Singleton(); } return instance ; } public void print(){ System.out.println("www.Tim.com"); } }
单例模式(懒汉式)
最新推荐文章于 2022-04-22 17:50:00 发布