单例模式:一个对象只能创建一个实例。
为了确保单例实例的唯一性,所有的单例构造器都要被声明为私有的,再通过声明静态方法实现全局访问获得该单例实例。
懒汉式单例模式
public class Singleton {
//懒汉式这里没有马上new instance
private static Singleton instance = null;
private Singleton(){
System.out.println();
}
private static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
懒汉式可以理解为懒,没有马上用到,没有马上实例化,等调用了getInstance时,才去实例化。
饿汉式单例模式
public class Singleton {
//饿汉式这里马上实例化
private static Singleton instance =new Singleton();
private Singleton(){
System.out.println();
}
private static Singleton getInstance(){
return instance;
}
}
饿汉式可以理解为饥不择食,一运行马上就实例化了。
懒汉式是线程不安全的,饿汉式是线程安全的。
饿汉式为啥是线程安全的
private static Singleton instance =new Singleton();
饿汉式由static修饰,并且实例化交给了自己的引用。由于单例类被加载时,就已经实例化了。一个类只加载一次,所以这个只会创建唯一的一个实例。也就是严格的线程安全。
懒汉式为啥是线程不安全的
private static Singleton instance = null;
懒汉式在类加载的时候实例的引用是null,开始并没有实例化所以引用为null。
在多线程中用到懒汉式,可能会出现,第一个线程调用了构造函数实例化单例对象,而此时第二个线程来了,也会去检查实例是不是null,但是这个时候第一个线程还没有完成实例化操作,所以第二个线程得到的实例也是null,也会开始实例化单例对象。
同步锁 synchronized
有两种方式
第一种是声明synchronized 关键字
private static synchronized Singleton getInstance()
public class Singleton {
//懒汉式这里没有马上new instance
private static Singleton instance = null;
private Singleton(){
System.out.println();
}
private static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
第二种是synchronized 方法块
synchronized(Singleton.class){
if(instance == null){
instance = new Singleton();
}
}
public class Singleton {
//懒汉式这里没有马上new instance
private static Singleton instance = null;
private Singleton(){
System.out.println();
}
private static Singleton getInstance(){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
return instance;
}
}
上面的方法能够带来线程的安全,但是随之而来的是延迟。synchronized是线程同步的,如果实例已经创建了,同步锁就不起作用了。这里就需要双重校验的单例模式了。
双重检验机制的单例模式
if(instance == null){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
}
public class Singleton {
//懒汉式这里没有马上new instance
private static Singleton instance = null;
private Singleton(){
System.out.println();
}
private static Singleton getInstance(){
if(instance == null){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}