A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wnmmp 中级黑马   /  2014-8-8 15:57  /  678 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

正在学习《张孝祥Java多线程与并发库高级应用视频教程》中关于ThreadLocal类及应用技巧。。。懵了,为什么不能用MyThreadScopeData单例?而是用这个ThreadLocal的单例?
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 ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
        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 个回复

您需要登录后才可以回帖 登录 | 加入黑马