软件编程
位置:首页>> 软件编程>> java编程>> Java单例模式、饥饿模式代码实例

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

作者:junjie  发布时间:2022-04-15 22:22:06 

标签: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;
    }
}
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com