/**
* 懒汉式,会有线程并发问题,所以要加线程锁
*/
public class SimpleSingleton {
private SimpleSingleton() {
}
private static SimpleSingleton singleton = null;
public static synchronized SimpleSingleton getInstence() {
if (singleton == null) {
singleton = new SimpleSingleton();
}
return singleton;
}
}
/**
* 饿汉式
*
* @author Administrator
*
*/
class SimpleSingleton2 {
private SimpleSingleton2() {
}
private static SimpleSingleton2 singleton2 = new SimpleSingleton2();
public static SimpleSingleton2 getInstence() {
return singleton2;
}
}
java中的单例模式
最新推荐文章于 2019-06-13 09:06:10 发布