Home > Java > javaTutorial > Simple implementation of java singleton pattern

Simple implementation of java singleton pattern

黄舟
Release: 2017-03-10 11:38:52
Original
1152 people have browsed it


定义:确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例
场景:确保某个类只有一个对象的场景,避免产生多个对象消耗过多资源。如,访问IO、DB等资源时,就要考虑使用单例模式
Copy after login
class Singleton1 {// 饿汉式 
	private static Singleton1 intance = new Singleton1();//直接初始化
	private Singleton1() {}
	public static Singleton1 getInstance() {
		return intance;
	}
}

class Singleton2 {// 懒汉式
	private static Singleton2 intance;
	private Singleton2() {}
	public static Singleton2 getInstance() {//用到的时候 才加载
		if (intance == null) {
			intance = new Singleton2();
		}return intance;
	}
}

class Singleton3 {// 懒汉式 线程安全
	private static Singleton3 intance;
	private Singleton3() {}
	public synchronized static Singleton3 getInstance() {//用到的时候 才加载, 加锁  多线程调用,都有一个加锁的动作
		if (intance == null) {
			intance = new Singleton3();
		}
		return intance;
	}
}

class Singleton4 {// 懒汉式 线程安全
	private static Singleton4 intance;
	private Singleton4() {}
	public static Singleton4 getInstance() {//用到的时候 才加载
		synchronized (Singleton4.class) {// 加锁 
			if (intance == null) {
				intance = new Singleton4();
			}    
		}
		return intance;
	}
}

public class Singleton {//双重检查 懒汉式 线程安全
	private static Singleton intance;
	private Singleton() {}
	
	public static Singleton getInstance() {
		
		if (intance == null) {//检查1:如果为null,再执行到下一步加锁;不为null不执行加锁操作
			synchronized (Singleton.class) {//类级锁
				if (intance == null) {//检查2:为null,才new一个实例
					intance = new Singleton();
				}
			}
		}
		return intance;
	}
}
Copy after login


Static inner class singleton mode: lazy loading, thread safety

public class StaticInnerClassSingleton implements Serializable {

    private StaticInnerClassSingleton() {

    }

    public static StaticInnerClassSingleton getInstance() {
        return Builder.instance;
    }

    private static class Builder {
        private static StaticInnerClassSingleton instance = new StaticInnerClassSingleton();
    }

    private Object readResolve() throws ObjectStreamException {
        return Builder.instance;
    }
}
Copy after login

About the function: readResolve(), the hook function during deserialization. It is private and overridden to prevent the generation of new instances during deserialization

All the above singleton methods have this problem. Strictly speaking, they all need to be rewritten.

Enumeration: There can be attributes and methods. An enumeration of an element is It is singleton mode, Default thread safety

public enum  SingletonEnum {
    INSTANCE_A;

    private SingletonEnum() {//构造方法只能是private的,不写也是
        id = idGenerate++;
    }

    public String aa;

    public String getAa() {
        return aa;
    }

    private int idGenerate = 1000;
    private final int id;

    public int getId() {
        return id;
    }
}
Copy after login


The above is the detailed content of Simple implementation of java singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template