java 饥饿现象,Java单例模式、饥饿模式代码实例
class MyThreadScopeData {
// 單例
private MyThreadScopeData() {
}
// 提供獲取實例方法
public static synchronized MyThreadScopeData getThreadInstance() {
// 從當(dāng)前線程范圍內(nèi)數(shù)據(jù)集中獲取實例對象
MyThreadScopeData instance = map.get();
if (instance == null) {
instance = new MyThreadScopeData();
map.set(instance);
}
return instance;
}
// 將實例對象存入當(dāng)前線程范圍內(nèi)數(shù)據(jù)集中
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;
}
}
總結(jié)
以上是生活随笔為你收集整理的java 饥饿现象,Java单例模式、饥饿模式代码实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宜春看免疫性不孕症最好的医院推荐
- 下一篇: 北京环球影城巫师袍价格