Skip to content

Commit 2cfeabf

Browse files
author
‘mxg133’
committed
2020-8-8
1 parent a4a6d1f commit 2cfeabf

72 files changed

Lines changed: 3623 additions & 50 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package _3singleton;
2+
3+
/**
4+
* @author 孟享广
5+
* @date 2020-08-08 12:19 下午
6+
* @description
7+
*/
8+
public class Test {
9+
public static void main(String[] args) {
10+
11+
Runtime runtime = Runtime.getRuntime();
12+
System.out.println(runtime);
13+
}
14+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _3singleton.my;
1+
package _3singleton.type1.my;
22

33
/**
44
* @author 孟享广
@@ -7,8 +7,8 @@
77
*/
88
public class SingletonTest01 {
99
public static void main(String[] args) {
10-
_3singleton.type2.my.singleton instance = _3singleton.type2.my.singleton.getInstance();
11-
_3singleton.type2.my.singleton singleton2 = _3singleton.type2.my.singleton.getInstance();
10+
singleton instance = singleton.getInstance();
11+
singleton singleton2 = singleton.getInstance();
1212
System.out.println(instance.equals(singleton2));
1313

1414
}
@@ -17,9 +17,9 @@ public static void main(String[] args) {
1717
class singleton{
1818
private singleton() {
1919
}
20-
private final static _3singleton.type2.my.singleton instance = new _3singleton.type2.my.singleton();
20+
private final static singleton instance = new singleton();
2121

22-
public static _3singleton.type2.my.singleton getInstance(){
22+
public static singleton getInstance(){
2323
return instance;
2424
}
2525
}

DesignPattern/src/_3singleton/type3/SingletonTest03.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package com.atguigu.singleton.type3;
1+
package _3singleton.type3;
22

33

44
public class SingletonTest03 {
55

66
public static void main(String[] args) {
7-
System.out.println("懒汉式1 , 线程不安全~");
7+
System.out.println("懒汉式1 , 线程不安全~");
88
Singleton instance = Singleton.getInstance();
99
Singleton instance2 = Singleton.getInstance();
1010
System.out.println(instance == instance2); // true
@@ -16,11 +16,11 @@ public static void main(String[] args) {
1616

1717
class Singleton {
1818
private static Singleton instance;
19-
19+
2020
private Singleton() {}
21-
22-
//提供一个静态的公有方法,当使用到该方法时,才去创建 instance
23-
//即懒汉式
21+
22+
//提供一个静态的公有方法,当使用到该方法时,才去创建 instance
23+
//即懒汉式
2424
public static Singleton getInstance() {
2525
if(instance == null) {
2626
instance = new Singleton();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package _3singleton.type3.my;
2+
3+
/**
4+
* @author 孟享广
5+
* @date 2020-08-08 10:56 上午
6+
* @description
7+
*/
8+
public class SingletonTest03 {
9+
public static void main(String[] args) {
10+
singleton instance = singleton.getInstance();
11+
12+
}
13+
}
14+
15+
class singleton{
16+
private static singleton instance;
17+
18+
private singleton() {
19+
}
20+
21+
public static singleton getInstance(){
22+
if (instance == null){
23+
instance = new singleton();
24+
}
25+
return instance;
26+
}
27+
28+
29+
}

DesignPattern/src/_3singleton/type4/SingletonTest04.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package com.atguigu.singleton.type4;
2-
1+
package _3singleton.type4;
32

43
public class SingletonTest04 {
54

65
public static void main(String[] args) {
7-
System.out.println("懒汉式2 , 线程安全~");
6+
System.out.println("懒汉式2 , 线程安全~");
87
Singleton instance = Singleton.getInstance();
98
Singleton instance2 = Singleton.getInstance();
109
System.out.println(instance == instance2); // true
@@ -14,14 +13,14 @@ public static void main(String[] args) {
1413

1514
}
1615

17-
// 懒汉式(线程安全,同步方法)
16+
// 懒汉式(线程安全,同步方法)
1817
class Singleton {
1918
private static Singleton instance;
20-
19+
2120
private Singleton() {}
22-
23-
//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
24-
//即懒汉式
21+
22+
//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
23+
//即懒汉式
2524
public static synchronized Singleton getInstance() {
2625
if(instance == null) {
2726
instance = new Singleton();

DesignPattern/src/_3singleton/type6/SingletonTest06.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
package com.atguigu.singleton.type6;
2-
1+
package _3singleton.type6;
32

43
public class SingletonTest06 {
54

65
public static void main(String[] args) {
7-
System.out.println("双重检查");
6+
System.out.println("双重检查");
87
Singleton instance = Singleton.getInstance();
98
Singleton instance2 = Singleton.getInstance();
109
System.out.println(instance == instance2); // true
1110
System.out.println("instance.hashCode=" + instance.hashCode());
1211
System.out.println("instance2.hashCode=" + instance2.hashCode());
13-
1412
}
15-
1613
}
1714

18-
// 懒汉式(线程安全,同步方法)
15+
// 懒汉式(线程安全,同步方法)
1916
class Singleton {
2017
private static volatile Singleton instance;
21-
18+
2219
private Singleton() {}
23-
24-
//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题
25-
//同时保证了效率, 推荐使用
26-
20+
21+
//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题
22+
//同时保证了效率, 推荐使用
23+
2724
public static synchronized Singleton getInstance() {
2825
if(instance == null) {
2926
synchronized (Singleton.class) {
3027
if(instance == null) {
3128
instance = new Singleton();
3229
}
3330
}
34-
31+
3532
}
3633
return instance;
3734
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
package com.atguigu.singleton.type7;
2-
1+
package _3singleton.type7;
32

43
public class SingletonTest07 {
54

65
public static void main(String[] args) {
7-
System.out.println("使用静态内部类完成单例模式");
6+
System.out.println("使用静态内部类完成单例模式");
87
Singleton instance = Singleton.getInstance();
98
Singleton instance2 = Singleton.getInstance();
109
System.out.println(instance == instance2); // true
1110
System.out.println("instance.hashCode=" + instance.hashCode());
1211
System.out.println("instance2.hashCode=" + instance2.hashCode());
13-
12+
1413
}
1514

1615
}
1716

18-
// 静态内部类完成, 推荐使用
17+
// 静态内部类完成, 推荐使用
1918
class Singleton {
2019
private static volatile Singleton instance;
21-
22-
//构造器私有化
20+
21+
//构造器私有化
2322
private Singleton() {}
24-
25-
//写一个静态内部类,该类中有一个静态属性 Singleton
23+
24+
//写一个静态内部类,该类中有一个静态属性 Singleton
2625
private static class SingletonInstance {
27-
private static final Singleton INSTANCE = new Singleton();
26+
private static final Singleton INSTANCE = new Singleton();
2827
}
29-
30-
//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
31-
28+
29+
//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
30+
3231
public static synchronized Singleton getInstance() {
33-
32+
3433
return SingletonInstance.INSTANCE;
3534
}
3635
}

DesignPattern/src/_3singleton/type8/SingletonTest08.java renamed to DesignPattern/src/_3singleton/type8_Enum/SingletonTest08.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
package com.atguigu.singleton.type8;
1+
package _3singleton.type8_Enum;
22

33
public class SingletonTest08 {
44
public static void main(String[] args) {
55
Singleton instance = Singleton.INSTANCE;
66
Singleton instance2 = Singleton.INSTANCE;
77
System.out.println(instance == instance2);
8-
8+
99
System.out.println(instance.hashCode());
1010
System.out.println(instance2.hashCode());
11-
11+
1212
instance.sayOK();
1313
}
1414
}
1515

16-
//使用枚举,可以实现单例, 推荐
16+
//使用枚举,可以实现单例, 推荐
1717
enum Singleton {
18-
INSTANCE; //属性
18+
INSTANCE; //属性
1919
public void sayOK() {
2020
System.out.println("ok~");
2121
}

0 commit comments

Comments
 (0)