信息发布→ 登录 注册 退出

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

发布时间:2026-01-11

点击量:
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;
    }
}

在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!