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
    
  • 相关阅读:
    C#编程利器之二:结构与枚举(Structure and enumeration)
    解读设计模式模板方法模式(Template Method),电脑就是这样造出来的
    清空mysql一个库中的所有表
    在执行并行程序工程中,突然弹出 connection closed 窗口,随后 ssh 与服务器的连接断开,并行程序也中断
    菜鸟求救 myeclipse安装flex3插件的问题
    linux 下 将 shell script 与 一个桌面图标联系在一起 (2)
    MYSQL EXPLAIN语句的extended 选项学习体会
    MySQL 性能跟踪语句
    Flex Flash
    Flex Builder 3 正式版
  • 原文地址:https://www.cnblogs.com/icelan/p/9683034.html
Copyright © 2011-2022 走看看