欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java单例模式、饥饿模式代码实例

程序员文章站 2024-03-04 18:46:48
class mythreadscopedata {       // 单例   &nbs...
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;
    }
}