单例的8种设计模式,以及它们的优缺点
单例模式1 推荐使用
/**
* 饿汉式
* 类加载到内存后,实例化一个对象,JVM保证线程安全
* 简单实用,推荐使用
* 缺点:不管有没有用到,类加载时完成实例化
* @since 2022/11/15
*/
public class Singleton01 {
private static final Singleton01 INSTANCE = new Singleton01();
private Singleton01(){
}
public static Singleton01 getInstance(){
return INSTANCE;
}
}
单例模式2
/**
* 饿汉式2
* 类加载到内存后,实例化一个对象,JVM保证线程安全
* 缺点:不管有没有用到,类加载时完成实例化
* @since 2022/11/15
*/
public class Singleton02 {
private Singleton02(){}
private static final Singleton02 INSTANCE;
static {
INSTANCE = new Singleton02();
}
public static Singleton02 getInstance(){
return INSTANCE;
}
}
单例模式3
/**
* 懒汉式
* 达到了按需加载的需求,却带来了线程安全问题
* @since 2022/11/15
*/
public class Singleton03 {
private Singleton03(){}
private static Singleton03 INSTANCE;
public static Singleton03 getInstance(){
if(INSTANCE==null){
//模拟多线程情况
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton03();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i <100 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton03.getInstance().hashCode());
}
}).start();
}
}
}
单例模式4
/**
* 懒汉式
* 达到了按需加载的需求,却带来了线程安全问题
* 使用 synchronized 实现了线程安全,却带来了效率问题
* @since 2022/11/15
*/
public class Singleton04 {
private Singleton04(){}
private static Singleton04 INSTANCE;
public static synchronized Singleton04 getInstance(){
if(INSTANCE==null){
//模拟多线程情况
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton04();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i <100 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton04.getInstance().hashCode());
}
}).start();
}
}
}
单例模式5
/**
* 懒汉式
* 达到了按需加载的需求,却带来了线程安全问题
* 想通过减小同步代码块的方式提升效率
* 但是该方式仍然线程不安全
* @since 2022/11/15
*/
public class Singleton05 {
private Singleton05(){}
private static Singleton05 INSTANCE;
public static Singleton05 getInstance(){
if(INSTANCE==null){
//想通过减小同步代码块的方式提升效率,但是该方式仍然线程不安全
synchronized(Singleton05.class){
//模拟多线程情况
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton05();
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i <100 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton05.getInstance().hashCode());
}
}).start();
}
}
}
单例模式6
/**
* 懒汉式
* 达到了按需加载的需求,却带来了线程安全问题
* 使用 synchronized 双重检测来实现线程安全及按需加载的目的
* 这种方式相对完美
* @since 2022/11/15
*/
public class Singleton06 {
private Singleton06(){}
private static Singleton06 INSTANCE;
public static Singleton06 getInstance(){
if(INSTANCE==null){
//双重检测来实现线程安全及按需加载的目的
synchronized(Singleton06.class){
if(INSTANCE==null){
//模拟多线程情况
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i <100 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton06.getInstance().hashCode());
}
}).start();
}
}
}
单例模式7
/**
* 静态内部类的方式
* JVM保证单例
* 加载外部类时不会加载内部类,可以实现懒加载
* 增加的复杂性
* @since 2022/11/15
*/
public class Singleton07 {
private Singleton07(){}
private static class Singleton07Holder{
private final static Singleton07 INSTANCE = new Singleton07();
}
public static Singleton07 getInstance(){
return Singleton07Holder.INSTANCE;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton07.getInstance().hashCode());
}).start();
}
}
}
单例模式8
/**
* 不仅解决了线程安全,还可以防止反序列化(枚举没有构造方法)
* @since 2022/11/15
*/
public enum Singleton08 {
INSTANCE;
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton08.INSTANCE.hashCode());
}).start();
}
}
}