Java 设计模式-单例模式 理论代码相结合

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;

}

}

结论及优缺:

  1. 优点:起到了懒加载的效果

  2. 缺点:线程不安全,如果在多线程下,第一个线程进入到if(singleton==null)下,但还未来的及执行,第二个线程就紧随而来也进入了if(singleton==null)下,那么就会创建多个实例。就不是单例模式了。

  3. 建议:开发不要使用这种方式,线程安全问题不能解决,就是😱。

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) {

//懒汉式 线程不安全方式,适合单线程使用

//=单线程下是安全的,代码同上=

// 需要zi料+ 绿色徽【vip1024b】

=模拟多线程下=====

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;

/**

  • 提供一个公有的方法

  • 当使用到这个方法时,才去创建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;

}

}

结论及优缺:

最后

由于篇幅原因,就不多做展示了
添加 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;

}

}

结论及优缺:

最后

[外链图片转存中…(img-jgigcAk4-1710351017618)]

[外链图片转存中…(img-TSb7gUtD-1710351017619)]

[外链图片转存中…(img-OCtXUZRh-1710351017620)]

[外链图片转存中…(img-md8Ilrj4-1710351017620)]

[外链图片转存中…(img-GH0EsZOT-1710351017620)]

[外链图片转存中…(img-WS8hhBjk-1710351017621)]

由于篇幅原因,就不多做展示了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值