单例模式

2020年4月初,网易笔试考到了单例模式;4月22日,阿里总监面又问到了设计模式;几天之后,我同学面拼多多,面试官又让他手写单例模式。在面试过程中,单例模式被考察到的频率实在太高了。

饿汉式

线程安全

1
2
3
4
5
6
7
8
9
class Singleton {
private static Singleton instance = new Singleton();

public static Singleton getInstance() {
return instance;
}

private Singleton() {}
}

典型示例:java.lang.Runtime

1
2
3
4
5
6
7
8
9
10
public class Runtime {
private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() {
return currentRuntime;
}

private Runtime() {}
// 省略了其他代码
}

懒汉式

线程不安全。

1
2
3
4
5
6
7
8
9
10
11
12
class Singleton {
private static Singleton instance;

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

private Singleton() {}
}

加锁

1.同步方法

1
2
3
4
5
6
7
8
9
10
11
12
class Singleton {
private static Singleton instance;

public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

private Singleton() {}
}

2.同步代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Singleton {
private static Singleton instance;

public static Singleton getInstance() {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

private Singleton() {}
}

DCL

双重锁定检查(Double Check Locking,简称DCL),线程安全。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Singleton {
// volatile保证可见性
private static volatile Singleton instance;

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}

private Singleton() {}
}

静态代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
class Singleton {
private static Singleton instance;

static {
instance = new Singleton();
}

public static Singleton getInstance() {
return instance;
}

private Singleton() {}
}

静态内部类

1
2
3
4
5
6
7
8
9
10
11
12
class Singleton {
private Singleton() {}

// 私有的静态内部类
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

枚举

1
2
3
enum Singleton {
INSTANCE;
}

----------本文结束感谢您的阅读----------
坚持原创技术分享,您的支持将鼓励我继续创作!