Java單例形式、饑餓形式代碼實例。本站提示廣大學習愛好者:(Java單例形式、饑餓形式代碼實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java單例形式、饑餓形式代碼實例正文
class MyThreadScopeData {
// 單例
private MyThreadScopeData() {
}
// 供給獲得實例辦法
public static synchronized MyThreadScopeData getThreadInstance() {
// 從以後線程規模內數據集中獲得實例對象
MyThreadScopeData instance = map.get();
if (instance == null) {
instance = new MyThreadScopeData();
map.set(instance);
}
return instance;
}
// 將實例對象存入以後線程規模內數據集中
private static MyThreadScopeData instance = null; // 饑餓形式
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}