本文收录至博客专题:《深入研究 Java.lang 的类》
2007-11-23 22:55:11
已被深深的吸引
2009-03-30 14:30:34
我测试了下 如果不调用getStudent() 结果也没有区别么
2009-09-23 23:54:29
写的不错!
我也写了一遍,请指点http://danni505.blog.51cto.com/15547/204633
我也写了一遍,请指点http://danni505.blog.51cto.com/15547/204633
2009-09-23 23:57:33
利用ThreadLocal来做多线程环境下的管理器真是太适合了!
2009-09-28 11:48:06
貌似转载javaeye中的
2009-11-09 10:46:29
是好资料 不过方法得注释翻译得真烂
2010-03-08 16:43:06
Student student = getStudent();
改成
Student student = new Student();
好像是没有什么区别,也能区分每个线程的值
改成
Student student = new Student();
好像是没有什么区别,也能区分每个线程的值
2010-05-15 15:02:03
LZ根本就没有理解ThreadLocal
2010-10-29 17:00:02
8楼不要过早下结论
我毫不犹豫支持lz
lz抓住了其他我看过的绝大多数文章没有提到的步骤中的2.
2中的new其实是ThreadLocal的核心.
不得不说终于有明白人了.
我毫不犹豫支持lz
lz抓住了其他我看过的绝大多数文章没有提到的步骤中的2.
2中的new其实是ThreadLocal的核心.
不得不说终于有明白人了.
2010-12-16 19:55:31
学习了
例子写的真不错
例子写的真不错
2011-05-19 14:21:50
LZ或许明白了,但是还没说到点子上,或者说讲的不够明确。
2011-08-17 11:46:10
其实LZ student只是方法级别的变量,还不是线程级别的变量,例子不好
2011-10-13 13:35:48
12 楼正解
student 应该是线程级别的变量
楼主的方法内student不能诠释threadLocal 的作用
会误导别人
student 应该是线程级别的变量
楼主的方法内student不能诠释threadLocal 的作用
会误导别人
2012-05-23 15:16:10
其实你那个学生的例子是有问题的
2012-05-23 15:27:06
我给你改进一下吧 你的例子是有问题的其他都不变就是修改业务那个方法
/**
* 示例业务方法,用来测试
*/
public void accessStudent() {
//获取当前线程的名字
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
//产生一个随机数并打印
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
//获取一个Student对象,并将随机数年龄插入到对象属性中
getStudent().setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + getStudent().getAge());
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("thread " + currentThreadName + " second read age is:" + getStudent().getAge());
}
你的student的是一个局部变量 取局部变量的话 永远在方法中都不会改变的
/**
* 示例业务方法,用来测试
*/
public void accessStudent() {
//获取当前线程的名字
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
//产生一个随机数并打印
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
//获取一个Student对象,并将随机数年龄插入到对象属性中
getStudent().setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + getStudent().getAge());
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("thread " + currentThreadName + " second read age is:" + getStudent().getAge());
}
你的student的是一个局部变量 取局部变量的话 永远在方法中都不会改变的
2012-05-23 15:27:49
不信的话你将例子改成
protected Student getStudent() {
return new Student();
}
protected Student getStudent() {
return new Student();
}
2013-01-23 10:40:49
很好,写的不够详细!
2013-06-20 15:39:14
设置一个私有变量
private Student student;
......
protected Student getStudent() {
//Student
if (useLocal) {
student = (Student) studentLocal.get();
//studentLocal.get()null
if (student == null) {
//StudentstudentLocal
student = new Student();
studentLocal.set(student);
}
} else {
if(student == null)
student = new Student();
}
return student;
}
private Student student;
......
protected Student getStudent() {
//Student
if (useLocal) {
student = (Student) studentLocal.get();
//studentLocal.get()null
if (student == null) {
//StudentstudentLocal
student = new Student();
studentLocal.set(student);
}
} else {
if(student == null)
student = new Student();
}
return student;
}
2013-09-20 21:46:23
ThreadLocal在1.6版本中不是用一个全局的Map来存各个线程的变量副本,而是在Thread类中有一个ThreadLocalMap的变量,然后用Thread.currentThread().threadLocals.get(this)来引用的各线程变量副本,这样避免了去同步全局的Map
2014-07-14 22:01:40
转载保存到我的博客中了,houzhiqingjava.blog.163.com 如有冒犯请联系,以标明出处和作者。