zoukankan      html  css  js  c++  java
  • 设计模式之单例模式

    设计模式-单例模式

    在Java设计模式中,单例模式相对来说算是比较简单的一种构建模式。适用的场景在于:对于定义的一个类,在整个应用程序执行期间只有唯一的一个实例对象。

    主要实现方式包括饿汉式、懒汉式;懒汉式需要注意线程安全问题。
    核心是理解synchronize和volatile关键字。

    /**
     * 饿汉模式
     */
    public class Singleton {
        private static volatile Singleton instance = new Singleton();
    
        private Singleton() {
        }
    
        public static Singleton getInstance() {
            return instance;
        }
    }
    
    /**
     * 饿汉模式测试
     */
    public class Test implements Runnable {
        @Override
        public void run() {
            Singleton singleton = Singleton.getInstance();
            System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
        }
    }
    
    Thread-0---1174385069
    Thread-1---1174385069
    Thread-2---1174385069
    Thread-3---1174385069
    
    Process finished with exit code 0
    
    /**
     * 懒汉模式
     */
    public class Singleton {
        private static Singleton instance;
    
        private Singleton() {
        }
        
        public static Singleton getInstance() {
            if (instance == null)
                instance = new Singleton();
            return instance;
        }
    }
    
    /**
     * 懒汉模式测试
     */
    public class Test implements Runnable {
        @Override
        public void run() {
            Singleton singleton = Singleton.getInstance();
            System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
        }
    }
    线程不安全,可能产生多个实例
    Thread-2---1549208166
    Thread-0---1272861279
    Thread-1---1549208166
    Thread-3---1272861279
    
    Process finished with exit code 0
    
    /**
     * 懒汉模式 线程安全,instance需要volatile关键字修饰,并且双重验证+同
     * 步锁才能保证线程安全
     */
    public class Singleton {
        private static volatile Singleton instance;//volatile保证可见性
    
        private Singleton() {
        }
    
        public static Singleton getInstance() {
            /**
             * 双重验证加同步锁
             */
             if(instance == null){
                synchronized (Singleton.class) {
                if (instance == null)
                    instance = new Singleton();
                }
             }
            return instance;
        }
    }
    
    /**
     * 懒汉模式 线程安全 测试
     */
    public class Test implements Runnable {
        @Override
        public void run() {
            Singleton singleton = Singleton.getInstance();
            System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
            new Thread(test).start();
        }
    }
    Thread-1---33689776
    Thread-0---33689776
    Thread-2---33689776
    Thread-3---33689776
    
    Process finished with exit code 0
    
  • 相关阅读:
    以下文件中的行尾不一致。要将行尾标准化吗
    用户 NT AUTHORITYNETWORK SERVICE 登录失败
    sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
    附加数据库对于服务器失败(Microsoft.SqlServer.Smo),无法升级数据库,因为它是只读的,或者具有只读文件
    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法
    HTTP 错误 404.2
    vs智能提示突然消失的解决办法 (vs2008 vs2010 vs2012 智能提示)
    Visual Studio 常用快捷键
    403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOLASP.NET v4.0"问题
    短信轰炸PC版
  • 原文地址:https://www.cnblogs.com/icelan/p/9683034.html
Copyright © 2011-2022 走看看