非线程安全方式单例
缺点:非线程安全,不可取。
package com.wenxiaowu.dp.singleton;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CountDownLatch;
/**
* 非线程安全方式单例
*/
class NotSafeSingleton {
private NotSafeSingleton() {
}
public static NotSafeSingleton instance;
public static NotSafeSingleton getInstance() {
if (instance == null) {
instance = new NotSafeSingleton();
}
return instance;
}
public static void setNull() {
instance = null;
}
}
public class NotSafeSingletonTest {
public static void main(String[] args) throws InterruptedException {
while (true) {
Set<String> nameSet = new CopyOnWriteArraySet<>();
CountDownLatch countDownLatch = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
new Thread(() -> {
NotSafeSingleton instance = NotSafeSingleton.getInstance();
nameSet.add(instance.toString());
countDownLatch.countDown();
}).start();
}
countDownLatch.await();
if (nameSet.size() == 2) {
System.out.println(String.format("instanceNameSet: %s", nameSet.toString()));
break;
} else {
NotSafeSingleton.setNull();
}
}
}
}
懒汉式单例
优点:线程安全, 可以延迟加载
缺点:调用效率不高
package com.wenxiaowu.dp.singleton;
/**
* 懒汉式单例
* 线程安全但效率低下
*/
class LanhanSingleton {
private LanhanSingleton() {
}
public static LanhanSingleton instance;
/**
* 获取实例
* @return
*/
public static LanhanSingleton getInstance() {
if (instance == null) {
instance = new LanhanSingleton();
}
return instance;
}
}
public class LanhanSingletonTest {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
new Thread(() -> {
LanhanSingleton instance = LanhanSingleton.getInstance();
System.out.println(String.format("instanceName: %s", instance.toString()));
}).start();
}
}
}
双重校验锁式单例
优点:线程安全,调用效率高,可以延迟加载
缺点:-
package com.wenxiaowu.dp.singleton;
/**
* 双重检查
*/
class DoubleCheckSingleton {
private DoubleCheckSingleton() {
}
public static volatile DoubleCheckSingleton instance;
/**
* 获取实例
* 加入双重检查代码,解决线程不安全问题,解决懒加载问题
* @return
*/
public static DoubleCheckSingleton getInstance() {
if (instance == null) {
synchronized (DoubleCheckSingleton.class) {
if (instance == null) {
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}
}
public class DoubleCheckSingletonTest {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
new Thread(() -> {
DoubleCheckSingleton instance = DoubleCheckSingleton.getInstance();
System.out.println(String.format("instanceName: %s", instance.toString()));
}).start();
}
}
}
静态内部类单例
优点:线程安全,调用效率高,可以延迟加载
缺点:-
package com.wenxiaowu.dp.singleton;
/**
* 静态内部类
*/
class StaticInnerClassSingleton {
private static class InnerClass {
private static final StaticInnerClassSingleton INSTANCE = new StaticInnerClassSingleton();
}
private StaticInnerClassSingleton() {
}
/**
* 获取实例
* @return
*/
public static StaticInnerClassSingleton getInstance() {
return InnerClass.INSTANCE;
}
}
public class StaticInnerClassSingletonTest {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
new Thread(() -> {
StaticInnerClassSingleton instance = StaticInnerClassSingleton.getInstance();
System.out.println(String.format("instanceName: %s", instance.toString()));
}).start();
}
}
}
枚举方式单例
优点:线程安全,调用效率高
缺点:不能延迟加载,存在多个类加载器生成多个对象的可能性
package com.wenxiaowu.dp.singleton;
/**
* 使用枚举可以实现单例设计模式,推荐使用
* 但是在多个类加载器中会存在实例多个对象的可能性
*/
enum EnumSingleton{
INSTANCE;
}
public class EnumSingletonTest {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
new Thread(() -> {
EnumSingleton instance = EnumSingleton.INSTANCE;
System.out.println(String.format("instanceName: %s", instance.toString()));
}).start();
}
}
}