From 6a2453918d27ce32600acbe3264d05c3c12b8df9 Mon Sep 17 00:00:00 2001 From: colining <18810782954@163.com> Date: Sun, 7 May 2017 21:09:17 +0800 Subject: [PATCH 1/2] Update Test02.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 单例的懒汉和饿汉写反啦; --- src/Test02.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Test02.java b/src/Test02.java index 6034b11..3f01a9d 100644 --- a/src/Test02.java +++ b/src/Test02.java @@ -1,5 +1,5 @@ /** - * Author: + * Author: 王俊超 * Date: 2015-04-21 * Time: 13:58 * Declaration: All Rights Reserved !!! @@ -7,7 +7,8 @@ public class Test02 { /** - * ģʽʽ̰߳ȫ + * 单例模式,饿汉式,线程安全 + * */ public static class Singleton { private final static Singleton INSTANCE = new Singleton(); @@ -22,7 +23,8 @@ public static Singleton getInstance() { } /** - * ģʽʽ̲߳ȫ + * 单例模式,懒汉式,线程不安全 + * 懒汉,用到才初始化,所以懒汉才会出现线程安全问题啊 */ public static class Singleton2 { private static Singleton2 instance = null; @@ -42,7 +44,7 @@ public static Singleton2 getInstance() { /** - * ģʽʽ̰߳ȫ̻߳Чʲ + * 单例模式,懒汉式,线程安全,多线程环境下效率不高 */ public static class Singleton3 { private static Singleton3 instance = null; @@ -61,7 +63,7 @@ public static synchronized Singleton3 getInstance() { } /** - * ģʽʽ֣̰߳ȫ + * 单例模式,懒汉式,变种,线程安全 */ public static class Singleton4 { private static Singleton4 instance = null; @@ -80,7 +82,7 @@ public static Singleton4 getInstance() { } /** - * ģʽʽʹþ̬ڲ̰࣬߳ȫƼ + * 单例模式,懒汉式,使用静态内部类,线程安全【推荐】 */ public static class Singleton5 { private final static class SingletonHolder { @@ -97,7 +99,7 @@ public static Singleton5 getInstance() { } /** - * ̬ڲ࣬ʹöٷʽ̰߳ȫƼ + * 静态内部类,使用枚举方式,线程安全【推荐】 */ public enum Singleton6 { INSTANCE; @@ -108,7 +110,7 @@ public void whateverMethod() { } /** - * ̬ڲ࣬ʹ˫У̰߳ȫƼ + * 静态内部类,使用双重校验锁,线程安全【推荐】 */ public static class Singleton7 { private volatile static Singleton7 instance = null; From d49751fddcc160a631d4b6df283109ccfa1fabf1 Mon Sep 17 00:00:00 2001 From: colining <18810782954@163.com> Date: Sun, 7 May 2017 21:36:09 +0800 Subject: [PATCH 2/2] Update Test02.java --- src/Test02.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Test02.java b/src/Test02.java index 3f01a9d..b00fc55 100644 --- a/src/Test02.java +++ b/src/Test02.java @@ -63,7 +63,8 @@ public static synchronized Singleton3 getInstance() { } /** - * 单例模式,懒汉式,变种,线程安全 + * 单例模式,饿汉式,变种,线程安全 + * */ public static class Singleton4 { private static Singleton4 instance = null;