1、概念
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
2、八种单例模式
- 饿汉式(静态常量)
- 饿汉式(静态代码块)
- 懒汉式(线程不安全)
- 懒汉式(线程安全,同步方法)
- 懒汉式(线程不安全,同步代码块)
- 懒汉式(双重检查)
- 静态内部类
- 枚举
- 推荐使用饿汉式两种方式、懒汉式(双重检查)、静态内部类和枚举。
- 饿汉式有浪费内存的缺点。
3、代码
package com.mine.design.singleton;
public class SingletonTest {
public static void main(String[] args) throws Exception {
int num = 10;
// 饿汉式(静态常量)
System.out.println("=====饿汉式(静态常量)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton01 instance = Singleton01.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 饿汉式(静态代码块)
System.out.println("=====饿汉式(静态代码块)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton02 instance = Singleton02.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 懒汉式(线程不安全)
System.out.println("=====懒汉式(线程不安全)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton03 instance = Singleton03.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 懒汉式(线程安全,同步方法)
System.out.println("=====懒汉式(线程安全,同步方法)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton04 instance = Singleton04.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 懒汉式(线程不安全,同步代码块)
System.out.println("=====懒汉式(线程不安全,同步代码块)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton05 instance = Singleton05.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 懒汉式(双重检查)
System.out.println("=====懒汉式(双重检查)=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton06 instance = Singleton06.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 静态内部类
System.out.println("=====静态内部类=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton07 instance = Singleton07.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
// 枚举
System.out.println("=====枚举=====");
for (int i = 0; i < num; i++) {
new Thread(new Runnable() {
public void run() {
Singleton08 instance = Singleton08.getInstance();
System.out.println(instance.hashCode());
}
}).start();
}
Thread.sleep(1000);
}
}
// 饿汉式(静态常量)
class Singleton01 {
private final static Singleton01 instance = new Singleton01();
private Singleton01() {
}
public static Singleton01 getInstance() {
return instance;
}
}
// 饿汉式(静态代码块)
class Singleton02 {
private final static Singleton02 instance;
static {
instance = new Singleton02();
}
private Singleton02() {
}
public static Singleton02 getInstance() {
return instance;
}
}
// 懒汉式(线程不安全)
class Singleton03 {
private static Singleton03 instance;
private Singleton03() {
}
public static Singleton03 getInstance() {
if (instance == null) {
instance = new Singleton03();
}
return instance;
}
}
// 懒汉式(线程安全,同步方法)
class Singleton04 {
private static Singleton04 instance;
private Singleton04() {
}
public static synchronized Singleton04 getInstance() {
if (instance == null) {
instance = new Singleton04();
}
return instance;
}
}
// 懒汉式(线程不安全,同步代码块)
class Singleton05 {
private static Singleton05 instance;
private Singleton05() {
}
public static Singleton05 getInstance() {
if (instance == null) {
synchronized (Singleton05.class) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
instance = new Singleton05();
}
}
return instance;
}
}
// 懒汉式(双重检查)
class Singleton06 {
// private static Singleton06 instance;
private static volatile Singleton06 instance;
private Singleton06() {
}
public static Singleton06 getInstance() {
if (instance == null) {
synchronized (Singleton06.class) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (instance == null) {
instance = new Singleton06();
}
}
}
return instance;
}
}
// 静态内部类
class Singleton07 {
private Singleton07() {
}
private static class InnerClass {
private static Singleton07 instance = new Singleton07();
}
public static Singleton07 getInstance() {
return InnerClass.instance;
}
}
// 枚举
enum Singleton08 {
INSTANCE;
private Singleton08() {
}
public static Singleton08 getInstance() {
return INSTANCE;
}
}