}
}
/**
- 懒汉式 线程不安全方式
*/
class Singleton {
/*** 构造器私有化*/
private Singleton() {}
/*** 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。*/
private static Singleton singleton;
/**
-
提供一个公有的方法
-
当使用到这个方法时,才去创建singleton
*/
public static Singleton getInstance() {
if(singleton==null){
// 通过在这里堵赛的方式来模拟多线程
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
singleton= new Singleton();
}
return singleton;
}
}
结论及优缺:
-
优点:起到了懒加载的效果
-
缺点:线程不安全,如果在多线程下,第一个线程进入到
if(singleton==null)
下,但还未来的及执行,第二个线程就紧随而来也进入了if(singleton==null)
下,那么就会创建多个实例。就不是单例模式了。 -
建议:开发不要使用这种方式,线程安全问题不能解决,就是😱。
2.4、懒汉式(线程安全,同步方法)
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest4 {
public static void main(String[] args) {
//懒汉式 线程不安全方式,适合单线程使用
//=单线程下 单线程是安全的=
// =模拟多线程下=====
Runnable runnable = new Runnable(){
@Override
public void run() {
Singleton instance = Singleton.getInstance();
System.out.println(instance.hashCode());
}
};
Runnable runnable2 = new Runnable(){
@Override
public void run() {
Singleton instance1 = Singleton.getInstance();
System.out.println(instance1.hashCode());
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
/**
6902734
6902734
*/
}
}
/**
- 2、懒汉式 线程安全方式
*/
class Singleton {
/*** 构造器私有化 */
private Singleton() {}
/*** 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。*/
private static Singleton singleton;
/**
-
提供一个公有的方法
-
当使用到这个方法时,才去创建singleton
-
加上 synchronized 关键字 解决线程安全问题
*/
public static synchronized Singleton getInstance() {
if(singleton==null){
// 通过在这里堵赛的方式来模拟多线程
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
singleton= new Singleton();
}
return singleton;
}
}
结论及优缺:
-
其实代码和懒汉式线程不安全的实现,就是在
Singleton getInstance()
上多了一个了synchronized
,将这个方法变成了同步方法,解决了线程同步问题。 -
缺点:但是因为在方法上加了
synchronized
关键字,导致执行效率的降低。并且之后每次来获取,都要进行同步,但其实本质上这段代码执行一次,之后都是retrun 是最佳的,而方法进行同步就大大降低效率拉。 -
不推荐这种方式,虽然做到线程同步,但效率太低,影响使用。
2.5、懒汉式(线程并不安全的同步代码块)
package com.crush.singleton05;
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest5 {
public static void main(String[] args) {
//懒汉式 线程不安全方式,适合单线程使用
//=单线程下是安全的,代码同上=
// =模拟多线程下=====
Runnable runnable = new Runnable() {
@Override
public void run() {
Singleton instance = Singleton.getInstance();
System.out.println(instance.hashCode());
}
};
Runnable runnable2 = new Runnable() {
@Override
public void run() {
Singleton instance1 = Singleton.getInstance();
System.out.println(instance1.hashCode());
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
/**
- 如果这样的话 多线程是并不安全的。
20775718
5987586
*/
}
}
/**
- 2、懒汉式 网上有说这是线程安全的问题,也有说不是的
*/
class Singleton {
/** * 构造器私有化*/
private Singleton() {
}
/*** 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。 */
private static Singleton singleton;
/**
-
提供一个公有的方法
-
当使用到这个方法时,才去创建singleton
-
在同步代码块 处添加 synchronized
*/
public static Singleton getInstance() {
if (singleton == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
}
synchronized (Singleton.class) {
singleton = new Singleton();
}
}
return singleton;
}
}
结论及优缺:
1)其实本意是对上一种方式做一个优化,想要提高同步效率,改为这种同步代码块的方式
2)但实际上,这并不能起到线程同步的作用,跟上一种方式遇到的问题是一样的。
3)同样不建议采取这种方式来实现单例模式。
2.6、懒汉式(双重检查)
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest6 {
public static void main(String[] args) {
//懒汉式 线程安全方式,适合单、多线程使用
//=单线程下是安全的,代码同上=
// =模拟多线程下=====
Runnable runnable = new Runnable() {
@Override
public void run() {
Singleton instance = Singleton.getInstance();
System.out.println(instance.hashCode());
}
};
Runnable runnable2 = new Runnable() {
@Override
public void run() {
Singleton instance1 = Singleton.getInstance();
System.out.println(instance1.hashCode());
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
/**
-
线程安全
-
7739563
-
7739563
*/
}
}
/**
- 2、懒汉式 双重检查 线程安全
*/
class Singleton {
/*** 构造器私有化*/
private Singleton() {
}
/*** 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。*/
private static Singleton singleton;
/**
-
提供一个公有的方法
-
当使用到这个方法时,才去创建singleton
-
在同步代码块 处添加 synchronized
-
双重检查
*/
public static Singleton getInstance() {
if (singleton == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Singleton.class) {
if(singleton==null){
singleton = new Singleton();
}
}
}
return singleton;
}
}
结论及优缺:
1)我们在代码中进行了两次if (singleton == null)
操作,一次在同步代码块外,一次在同步代码块内,两次检查,保证了线程的安全。
2)这样的话,同步代码只要执行一次,singleton也只需实例化一次,既做到了懒加载,又同时保证线程安全,提高了执行效率。
3)结论:懒加载、线程安全、效率较高,当然用这种方式啊。
2.7、懒汉式(静态内部类)
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest7 {
public static void main(String[] args) {
//懒汉式 线程安全方式,适合单、多线程使用
//=单线程下是安全的 和上面一样的=
// =模拟多线程下=====
Runnable runnable = new Runnable() {
@Override
public void run() {
Singleton instance = Singleton.getInstance();
System.out.println(instance.hashCode());
}
};
Runnable runnable2 = new Runnable() {
@Override
public void run() {
Singleton instance1 = Singleton.getInstance();
System.out.println(instance1.hashCode());
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
/**
-
线程安全
-
7739563
-
7739563
*/
}
}
/**
- 2、懒汉式 静态内部类方式
*/
class Singleton {
/*** 构造器私有化*/
private Singleton() {
}
/** * 写一个静态内部类,然后在类的内部再写一个静态常量 Singleton */
private static class SingletonInstance {
private final static Singleton SINGLETON=new Singleton();
}
/**
-
提供一个公有的方法
-
当使用到这个方法时,才去加载 SingletonInstance 获得 Singleton 实例
*/
public static Singleton getInstance() {
return SingletonInstance.SINGLETON;
}
}
结论及优缺:
1)这种方式同样不会产生线程同步问题,也是借用JVM的类装载的机制来保证实例化的时候只有一个线程。
2)静态内部类SingletonInstance
在Singleton
被装载时,并不会立即实例化,而是在需要实例化的时候,调用了getInstance 方法,才会进行 SingletonInstance
类的装载。
3)类的静态属性只会在第一次加载类的时候进行初始化,而在这里,JVM的类装载机制帮助我们保证了线程安全性。
4)小结:避免了线程安全问题、利用了静态内部类延迟加载(做到懒加载)、效率高,这不更爽了吗,用起来。
2.8、枚举类实现
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14