aspose file tools*
The moose likes Threads and Synchronization and the fly likes confusion in ThreadLocal example. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "confusion in ThreadLocal example." Watch "confusion in ThreadLocal example." New topic
Author

confusion in ThreadLocal example.

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45



// 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

Joined: Mar 20, 2001
Posts: 210

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

Joined: Dec 18, 2007
Posts: 45

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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

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

Joined: Nov 13, 2006
Posts: 154
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

Joined: Dec 18, 2007
Posts: 45

ok thanks ajay.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

It is just the computation... for first it is doing for 199-19 and hence the result is 180. Rest is all the same,....


My Blog SCJP 5 SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion in ThreadLocal example.