栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

}

}

class Singleton {

private Singleton() {}

private static 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、懒汉式(线程安全,同步方法)

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();

}

}

class Singleton {

private Singleton() {}

private static Singleton singleton;

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;

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();

}

}

class Singleton {

private Singleton() {

}

private static Singleton singleton;

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、懒汉式(双重检查)

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();

}

}

class Singleton {

private Singleton() {

}

private static Singleton singleton;

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、懒汉式(静态内部类)

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();

}

}

class Singleton {

private Singleton() {

}

private static class SingletonInstance {

private final static Singleton SINGLETON=new 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号