2.1 题目
2.2 什么是 Singleton
- Singleton:在 Java 中即指单例设计模式,它是软件开发中最常用的设计模式之一
- 单:唯一
- 例:实例
- 单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式
-
2.3 要点
某个类只能有一个实例
- 它必须自行创建这个实例
- 含有一个该类的静态变量来保存这个唯一的实例
它必须自行向整个系统提供这个实例
饿汉式:直接创建对象,不存在线程安全问题
- 直接实例化饿汉式(简洁直观)
- 枚举式(最简洁)
- 静态代码块饿汉式(适合复杂实例化)
懒汉式:延迟创建对象
构造器私有化
- 自行创建,并且用静态变量保存
- 向外提供这个实例
- 强调这是一个单例,我们可以用 final 修改 ```java package s01.e02;
public class Singleton1 { public static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
}
枚举类型:表示该类型的对象是有限的几个,我们可以限定一个,就成了单例
```java
package s01.e02;
public enum Singleton2 {
INSTANCE
}
调用方法
package s01.e02;
public class TestSingleton1 {
public static void main(String[] args) {
Singleton1 s = Singleton1.INSTANCE;
System.out.println(s);
}
}
package s01.e02;
public class TestSingleton2 {
public static void main(String[] args) {
Singleton2 s = Singleton2.INSTANCE;
System.out.println(s);
}
}
静态代码块饿汉式
package s01.e02;
import java.io.IOException;
import java.util.Properties;
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;
static {
try {
Properties pro = new Properties();
pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
INSTANCE = new Singleton3(pro.getProperty("info"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Singleton3(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Singleton3{" +
"info='" + info + '\'' +
'}';
}
}
package s01.e02;
public class TestSingleton3 {
public static void main(String[] args) {
Singleton3 s = Singleton3.INSTANCE;
System.out.println(s);
}
}
info=atguigu
懒汉式:延迟创建这个实例对象
- 构造器私有化
- 用一个静态变量保存这个唯一的实例
- 提供一个静态方法,获取这个实例对象
单线程安全演示
package s01.e02;
public class Singleton4 {
private static Singleton4 instance;
private Singleton4() {
}
public static Singleton4 getInstance() {
if (instance == null) {
instance = new Singleton4();
}
return instance;
}
}
package s01.e02;
public class TestSingleton4 {
public static void main(String[] args) {
Singleton4 s1 = Singleton4.getInstance();
Singleton4 s2 = Singleton4.getInstance();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
}
}
线程不安全演示
package s01.e02;
public class Singleton4 {
private static Singleton4 instance;
private Singleton4() {
}
public static Singleton4 getInstance() {
if (instance == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton4();
}
return instance;
}
}
package s01.e02;
import java.util.concurrent.*;
public class TestSingleton4 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<Singleton4> c = Singleton4::getInstance;
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Singleton4> f1 = es.submit(c);
Future<Singleton4> f2 = es.submit(c);
Singleton4 s1 = f1.get();
Singleton4 s2 = f2.get();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
es.shutdown();
}
}
线程安全演示
package s01.e02;
public class Singleton5 {
private static Singleton5 instance;
private Singleton5() {
}
public static Singleton5 getInstance() {
if (instance == null) { // 优化性能,不为空时不需要锁住等待
synchronized (Singleton5.class) {
if (instance == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton5();
}
}
}
return instance;
}
}
package s01.e02;
import java.util.concurrent.*;
public class TestSingleton5 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<Singleton5> c = Singleton5::getInstance;
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Singleton5> f1 = es.submit(c);
Future<Singleton5> f2 = es.submit(c);
Singleton5 s1 = f1.get();
Singleton5 s2 = f2.get();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
es.shutdown();
}
}
静态内部类形式:在内部类被加载和初始化时,才创建 INSTANCE 实例对象
静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始化的
因为是在内部类加载和初始化时创建的,因此是线程安全的
package s01.e02;
public class Singleton6 {
private Singleton6() {
}
private static class Inner {
private static final Singleton6 INSTANCE = new Singleton6();
}
public static Singleton6 getInstance() {
return Inner.INSTANCE;
}
}
2.6 小结
- 如果是饿汉式,枚举形式最简单
- 如果是懒汉式,静态内部类形式最简单