zoukankan      html  css  js  c++  java
  • 单例模式(Singleton)的同步锁synchronized

    单例模式,有“懒汉式”和“饿汉式”两种。

    懒汉式

    单例类的实例在第一次被引用时候才被初始化。

    public class Singleton {  
        private static Singleton instance=null;  
         
        private Singleton() {  
             
        }  
         
        public static Singleton getInstance(){  
            if (instance == null) {  
                instance = new Singleton();  
            }  
             
            return instance;  
        }  
    }

    饿汉式

    单例类的实例在加载的时候就被初始化。

    public class Singleton {  
        private static Singleton instance = new Singleton();  
         
        private Singleton() {  
             
        }  
         
        public static Singleton getInstance(){  
            return instance;  
        }  
    }

    在单线程程序中,上面两种形式基本可以满足要求了,但是在多线程环境下,单例类就有可能会失效,这个时候就要对其加锁了,来确保线程安全。

    对线程加锁用的synchronized关键字,这个关键字的用法主要也分为两种:

    一种是加在方法名之前,形如:synchronized methodeName(params){……};

    二是声明同步块,形如:synchronized(this){……};

    下面是对懒汉式单例类加上线程同步的实现:

    同步方法:

    public class Singleton {  
        private static Singleton instance=null;  
         
        private Singleton() {  
             
        }  
         
        public synchronized static Singleton getInstance(){  
            if (instance == null) {  
                instance = new Singleton();  
            }  
             
            return instance;  
        }  
    }

    这种方式效率比较低,性能不是太好,不过也可以用,因为是对整个方法加上了线程同步,其实只要在new的时候考虑线程同步就行了,这种方法不推荐使用。

    同步代码块:

    public class Singleton {  
        private static Singleton instance;  
        private final static Object syncLock = new Object();  
         
        private Singleton() {  
             
        }  
         
        public static Singleton getInstance(){  
            if (instance == null) {  
                synchronized (syncLock) {  
                    if (instance == null) {  
                        instance = new Singleton();  
                    }  
                }  
            }  
             
            return instance;  
        }  
    }

    synchronized同步块括号中的锁定对象是采用的一个无关的Object类实例,而不是采用this,因为getInstance是一个静态方法,在它内部不能使用未静态的或者未实例的类对象,因此也可以用下面的方法来实现:

    public class Singleton {  
        private static Singleton instance;  
         
        private Singleton() {  
             
        }  
         
        public static Singleton getInstance(){  
            if (instance == null) {  
                synchronized (Singleton.class) {  
                    if (instance == null) {  
                        instance = new Singleton();  
                    }  
                }  
            }  
             
            return instance;  
        }  
    }
  • 相关阅读:
    Python-学习-小例子练习
    Python-学习-import语句导入模块
    Python-类-函数参数-takes 0 positional arguments but 1 was given
    虚拟现实-VR-UE4-构建光照显示光照构建失败,Swarm启动失败
    Qt 实现脉搏检测-2,简陋的功能产品
    Qt 蓝牙部分翻译
    Qt QLabel 播放GIF动画
    Qt 实现脉搏检测-1-心跳曲线部分
    C++学习014函数值传递和地址传递
    Ubuntu 普通用户无法启动Google chrome
  • 原文地址:https://www.cnblogs.com/qq895139140/p/7774152.html
Copyright © 2011-2022 走看看