Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion in ThreadLocal example.

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


// are the output and the sequence in wich the output is generated is

null
null
80
20
100
40
1


Now i am not getting that y the first two output�s are null when the value of t in main is set to 1 and then that reference variable is passed as an argument to the objects of class Thread1 & Thread2 where i am setting the local reference variable of each class to this main class t.So im my opinion the output should have been
1
1
80
20
100
40
1
[ September 04, 2008: Message edited by: PUNEET MITTAL ]
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puneet,

The reason is because ThreadLocal value is ties to the current running thread, it is not like regular object.

When you set the value to 1, that value is associated with the main thread of your application.

When you are accesing them inside your own thread object, the ThreadLocal has no value yet associated with that thread which is why you get null
[ September 02, 2008: Message edited by: Wirianto Djunaidi ]
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks wirianto but then what is the use of these two constructors


and

because i believe that when i am passing t of main method while creating
object to Thread1 and Thread2 then the value of local t of Thread1 and Thread2 is getting initialized with this main method's t. And after initializing these values, we are using these local t for printing. So according to me it shud have printed the initialized values of t instead of null. because if it doesn't work that way as i am thinking than what is the use of giving these constructors. Actually i got this example in my test.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Wirianto Djunaidi said, setting the value of a ThreadLocal in one thread doesn't affect the value in other threads; that's the whole point. You set the value in one thread, read it in a second thread, and in the second thread, it will be null. It won't be non-null in the second thread until you set the value from the second thread!

There's a subclass of ThreadLocal called InheritableThreadLocal; in this version, the value in each thread is initialized to the value in the parent thread. Perhaps that's the behavior you're expecting. But the basic ThreadLocal doesn't do this.

Imagine a HashMap with Thread objects as the keys; that's really all a ThreadLocal is. There's a separate value for each thread.
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ernest but please can you clarify this point of yours:

You set the value in one thread, read it in a second thread, and in the second thread, it will be null. It won't be non-null in the second thread until you set the value from the second thread!
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method get() when called for the first time on a threadlocal object through some thread would invoke the initialValue() method. The default initialValue() implementation returns a null,which is why you are getting the observed behavior. In order to get what you are expecting, you either need to subclass ThreadLocal and override the initialValue() method,or use InheritableThreadLocal,as suggested by Ernest.

Also look at what JDK implementation doc has to say and the relevant code below.

This is directly from JDK implementation of ThreadLocal



[ September 05, 2008: Message edited by: Ajay Saxena ]
[ September 05, 2008: Message edited by: Ajay Saxena ]
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks ajay.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just the computation... for first it is doing for 199-19 and hence the result is 180. Rest is all the same,....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic