This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
// 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 ]
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 ]
thanks wirianto but then what is the use of these two constructors
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.
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.
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 ]