/** * 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。 */
private final static Singleton INSTANCE=new Singleton();
/*** 再提供一个 公有的方法来返回这个静态常量*/
public static Singleton getInstance(){
return INSTANCE;
}
}
结论及优缺:
-
优点:饿汉式(静态常量方式)它不用担心线程安全问题,它本身就是在类的装载的时候完成实例化的。
-
缺点:但是缺点也在这个地方,不管用不用,只要类装载了,那么他就会直接完成实例化,不能达到懒加载的效果,如果你从始至终都没有使用过这个类,那么就会造成内存的浪费。
注意
:你可能会想类都已经加载了,为什么我还会不用到呢?
在单例模式中大都调用getInstance方法,getInstance这个静态方法可以让类加载,那么同样的道理,如果这个类中还有其他的静态方法,你调用它的时候,同样会使类进行装载。
- 小结:这种单例方式,其实还是可以用的,至少不用担心线程同步问题,那种使用特别频繁的类用这种方式还是没啥问题的,除了可能会导致内存浪费,
2.2、饿汉式(静态代码块)
/**
-
单例模式 2
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest1 {
public static void main(String[] args) {
// 我们去拿两次,看获取到的对象 确实是单例的吗
Singleton singleton = Singleton.getInstance();
Singleton singleton1 = Singleton.getInstance();
System.out.println(singleton == singleton1);
System.out.println(“singleton hashcode:” + singleton.hashCode());
System.out.println(“singleton1 hashcode:” + singleton1.hashCode());
/**
-
输出:
-
true
-
singleton hashcode:24324022
-
singleton1 hashcode:24324022
*/
}
}
/**
- 1、饿汉式(静态代码块)代码实现
*/
class Singleton {
/** * 构造器私有化 */
private Singleton() {
}
/** * 在类的内部创建一个对象实例 随当前类加载而加载 没有线程安全问题。*/
private static Singleton singleton;
static {
//改为在静态代码块中 创建单例对象
singleton = new Singleton();
}
/** * 再提供一个 公有的方法来返回这个静态常量 */
public static Singleton getInstance() {
return singleton;
}
}
结论:这种方式其实和第一种非常类似,只是将类的实例化过程放进静态代码块而已。优缺点同饿汉式(静态常量)。
2.3、懒汉式(线程不安全)
/**
-
单例模式
-
@Author: crush
-
@Date: 2021-08-06 9:14
-
version 1.0
*/
public class SingletonTest3 {
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();
/**
-
结果并不唯一,
-
可能会出现相同,也有可能不同,多测几次,就能发现是线程不安全的。
-
94433
-
21648409
*/
}
}
/**
- 懒汉式 线程不安全方式
*/
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();
}
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;
Java面试核心知识点笔记
其中囊括了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。
Java中高级面试高频考点整理
最后分享Java进阶学习及面试必备的视频教学
on.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;
Java面试核心知识点笔记
其中囊括了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。
[外链图片转存中…(img-qZwqY7il-1719262137233)]
Java中高级面试高频考点整理
[外链图片转存中…(img-Z6dfxcSE-1719262137234)]
[外链图片转存中…(img-kzjCJXwv-1719262137235)]
最后分享Java进阶学习及面试必备的视频教学
[外链图片转存中…(img-12oimJwR-1719262137236)]