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
    
  • 相关阅读:
    Java中关于时间日期格式保存到mysql的问题
    传输json数据到前台的时候,数据中包含日期数据
    字符串的分隔方法 split()
    实体类中用基本类型好,还是用包装类型
    Android中的task和stack
    在res文件下新建文件夹
    Android活动和碎片的生命周期及addToBackStack()方法
    draw9patch图片拉伸
    Android studio新建文件出现setContentView(R.layout.activity_main);中的R标红错误解决方法
    listview控件的使用
  • 原文地址:https://www.cnblogs.com/icelan/p/9683034.html
Copyright © 2011-2022 走看看