标签:pack 模式 code sys click 赋值 初始化 style width
1 package com.cllover.lazy; 2 3 public class LazySingleton { 4 public static void main(String[] args) { 5 6 lazyinstance instance = lazyinstance.getInstance(); 7 System.out.println(instance+"\n"); 8 lazyinstance instance1 = lazyinstance.getInstance(); 9 System.out.println(instance1+"\n"); 10 11 12 } 13 } 14 15 16 class lazyinstance{ 17 18 private static lazyinstance instance; 19 20 private lazyinstance(){ 21 } 22 23 public static lazyinstance getInstance() { 24 if (instance == null){ 25 instance = new lazyinstance(); 26 } 27 return instance; 28 } 29 }
1 package com.cllover.lazy; 2 3 public class LazySingleton { 4 5 public static void main(String[] args) { 6 //线程一 7 new Thread(()->{ 8 lazyinstance instance = lazyinstance.getInstance(); 9 System.out.println(instance+"\n"); 10 }).start(); 11 //线程二 12 new Thread(()->{ 13 lazyinstance instance1 = lazyinstance.getInstance(); 14 System.out.println(instance1+"\n"); 15 }).start(); 16 } 17 } 18 class lazyinstance{ 19 20 private static lazyinstance instance; 21 22 private lazyinstance(){ 23 } 24 25 public static lazyinstance getInstance() { 26 27 if (instance == null){ 28 instance = new lazyinstance(); 29 } 30 return instance; 31 } 32 }
1 package com.cllover.lazy; 2 3 public class LazySingleton { 4 5 public static void main(String[] args) { 6 //线程一 7 new Thread(()->{ 8 lazyinstance instance = lazyinstance.getInstance(); 9 System.out.println(instance+"\n"); 10 }).start(); 11 //线程二 12 new Thread(()->{ 13 lazyinstance instance1 = lazyinstance.getInstance(); 14 System.out.println(instance1+"\n"); 15 }).start(); 16 //线程三 17 new Thread(()->{ 18 lazyinstance instance2 = lazyinstance.getInstance(); 19 System.out.println(instance2+"\n"); 20 }).start(); 21 } 22 } 23 class lazyinstance{ 24 25 private static lazyinstance instance; 26 27 private lazyinstance(){ 28 } 29 30 public static lazyinstance getInstance() { 31 32 if (instance == null){ 33 //同步堵塞 34 synchronized (lazyinstance.class){ 35 //是否进行过实例化 36 if (instance == null){ 37 instance = new lazyinstance(); 38 } 39 } 40 } 41 return instance; 42 } 43 }
标签:pack 模式 code sys click 赋值 初始化 style width
原文地址:https://www.cnblogs.com/CllOVER/p/13303009.html