java线程安全单例

public class MySingleton {
	// 使用volatile关键字保其可见性
	volatile private static MySingleton instance = null;

	private MySingleton() {
	}

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

  

posted @ 2017-03-03 17:29  谭志宇  阅读(192)  评论(0编辑  收藏  举报