java设计模式之单例模式(加锁的懒汉)
package src.test.java.com.pbc.singleton;
/**
* (加锁的懒汉式)单例模式
* @author qiu_y
*
*/
public class SingletonLock {
* (加锁的懒汉式)单例模式
* @author qiu_y
*
*/
public class SingletonLock {
private static SingletonLock singletonLock = null;
/**
* 私有构造器
*/
private SingletonLock() {}
/**
* 加锁的懒汉式单例
* 1.相比较懒汉式更加线程安全,缺由于加锁导致额外消耗
*/
public static SingletonLock getInstance() {
synchronized (SingletonLock.class) {
if (null == singletonLock) {
singletonLock = new SingletonLock();
}
}
return singletonLock;
}
}
/**
* 私有构造器
*/
private SingletonLock() {}
/**
* 加锁的懒汉式单例
* 1.相比较懒汉式更加线程安全,缺由于加锁导致额外消耗
*/
public static SingletonLock getInstance() {
synchronized (SingletonLock.class) {
if (null == singletonLock) {
singletonLock = new SingletonLock();
}
}
return singletonLock;
}
}