当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 设计模式-单例模式code

设计模式-单例模式code

2019年10月20日  | 移动技术网IT编程  | 我要评论
package singeton;


import java.security.securerandom;

/**
* @author zero
* @since 2019-08-13.
* description:
*/
public class hungrysingleton {
private static final hungrysingleton singleton = new hungrysingleton();
private final int id = new securerandom().nextint();

private hungrysingleton() {
}

public static hungrysingleton getsingleton() {
return singleton;
}

public int dosomething() {
// system.out.println("i'm hungrysingeton " + id + "!");
return id;
}
}

package singeton;

import java.security.securerandom;

/**
* @author zero
* @since 2019-08-13.
* description:
*/
public class lazysingleton {
private static lazysingleton singeton = null;
private final int id = new securerandom().nextint();

private lazysingleton() {
}

public static synchronized lazysingleton getsingleton() {
if (singeton == null) {
singeton = new lazysingleton();
}
return singeton;
}

public int dosomething() {
// system.out.println("i'm lazysingeton " + id + "!");
return id;
}

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网