黑马程序员技术交流社区

标题: ThreadLocal问题 [打印本页]

作者: 汪小照    时间: 2012-10-21 15:26
标题: ThreadLocal问题
本帖最后由 汪小照 于 2012-10-22 10:32 编辑
  1. import java.util.Random;

  2. /*
  3. * 当有多个数据时,将其封装成对象,
  4. * 存放的是对象,而不再是单个数据。
  5. */
  6. public class ThreadScopeShareDate1 {

  7. public static void main(String [] args)
  8. {
  9. for(int x=0;x<2;x++)
  10. {
  11. new Thread(new Runnable(){
  12. public void run()
  13. {
  14. int data = new Random().nextInt();
  15. System.out.println(Thread.currentThread()
  16. .getName()+" has put "+ data);
  17. MyThreadLocal mtl = MyThreadLocal.getInstance();
  18. mtl.setAge(data);
  19. mtl.setName("name"+ data);
  20. new A().get();
  21. new B().get();
  22. }
  23. }).start();
  24. }
  25. }
  26. static class A{
  27. public void get()
  28. {
  29. MyThreadLocal mtl = MyThreadLocal.getInstance();
  30. System.out.println("A form "+Thread.currentThread().getName()+
  31. " " + mtl.getAge()+" "+mtl.getName());
  32. }
  33. }
  34. static class B{
  35. public void get()
  36. {
  37. MyThreadLocal mtl = MyThreadLocal.getInstance();
  38. System.out.println("B from "+Thread.currentThread().getName()+
  39. " " + mtl.getAge()+" "+mtl.getName());
  40. }
  41. }
  42. }

  43. class MyThreadLocal{
  44. private String name;
  45. private int age;

  46. private static ThreadLocal<MyThreadLocal> tl = new ThreadLocal<MyThreadLocal>();
  47. private static MyThreadLocal instance =null;
  48. private MyThreadLocal(){}
  49. public static MyThreadLocal getInstance()
  50. {
  51. instance = tl.get();
  52. if(instance == null)
  53. {
  54. instance = new MyThreadLocal();
  55. tl.set(instance);
  56. }
  57. return instance;
  58. }

  59. public String getName() {
  60. return name;
  61. }
  62. public void setName(String name) {
  63. this.name = name;
  64. }
  65. public int getAge() {
  66. return age;
  67. }
  68. public void setAge(int age) {
  69. this.age = age;
  70. }

  71. }
复制代码
为什么打印的是这样的结果:
Thread-0 has put -1622789957
Thread-1 has put -1010639133
A form Thread-0  0   null
A form Thread-1  -1010639133   name-1010639133
B from Thread-0  0   null
B from Thread-1  -1010639133   name-1010639133

打印出来的竟然有0 和null ,而张老师讲课时运行多次都不会有0 和 null 的结果,有知道的请分析一下哪里出错了,错误的原因是什么?
作者: 给生活加点料    时间: 2012-10-21 15:47
站位。等回复。
作者: hello_world!    时间: 2012-10-21 16:30
  1. private static ThreadLocal<MyThreadLocal> tl = new ThreadLocal<MyThreadLocal>();
  2. [color=Red]private static MyThreadLocal instance =null; //关键是这句话引起的错误,你想过没有,你把返回值设置为静态的,在内存中就只有一份,你用的ThreadLocal根本就起不了作用。多线程可以同时访问,同时修改,所以结果不会出现互斥的现象,也没有实现数据共享[/color]
  3. private MyThreadLocal(){}
  4. public static MyThreadLocal getInstance()
  5. {
  6. instance = tl.get();
  7. if(instance == null)
  8. {
  9. instance = new MyThreadLocal();
  10. tl.set(instance);
  11. }
  12. return instance;
  13. }
复制代码
这样改就行了:
private static ThreadLocal<MyThreadLocal> tl = new ThreadLocal<MyThreadLocal>();
//private static MyThreadLocal instance =null;
private MyThreadLocal(){}
public static MyThreadLocal getInstance()
{
MyThreadLocal instance = tl.get();
if(instance == null)
{
instance = new MyThreadLocal();
tl.set(instance);
}
return instance;
}
作者: hello_world!    时间: 2012-10-21 16:31
忘记加上结果了:
Thread-0 has put -1824922109
A form Thread-0 -1824922109 name-1824922109
B from Thread-0 -1824922109 name-1824922109
Thread-1 has put -625868128
A form Thread-1 -625868128 name-625868128
B from Thread-1 -625868128 name-625868128
作者: 汪小照    时间: 2012-10-21 20:00
hello_world! 发表于 2012-10-21 16:31
忘记加上结果了:
Thread-0 has put -1824922109
A form Thread-0 -1824922109 name-1824922109

看明白了你所说的,谢谢啦




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2