aspose file tools*
The moose likes Threads and Synchronization and the fly likes Local variables always thread safe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Local variables always thread safe ?" Watch "Local variables always thread safe ?" New topic
Author

Local variables always thread safe ?

James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Hi all,

Are local variables always thread safe ? and if so why is this ?
My understanding is it is because they are not stored on the stack. But if they are not on the stack, where are they stored ?

thanks in advance,

J.C
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Local variables are thread-safe, in that no other thread can get at them. You may consider them to be stored on the stack for that thread, though Java programmers should rarely concern themselves about such low-level details.

You have to be careful not to read too much into this "thread-safe" nature of local variables. Most importantly, if your local variable is an object reference, then it is thread-safe only to the extent that another thread can't get in and change the object reference to point to another object. But, if another thread has its own reference to the same object, it CAN get in and change the object.

In Java, unlike C++ and some other languages, you cannot have an object as a local variable on a stack. Java objects are always on the heap.
[ August 15, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Just to expand on what Peter wrote, if you think of thread safety as applying to variables, you are thinking about it the wrong way. Thread safety is always about regulating simultaneous access to objects. So if you have a local variable and assign it an object reference like this:then it may contain a reference to an object that is also referenced by other variables in other threads. And in such a case you would need to consider thread safety issues. Whereas if you assigned it an object reference like this:then no other variable anywhere can contain a reference to that new object. In this case no thread safety issues arise -- until you assign a reference to that object to something outside the scope of the local variable...
[ August 15, 2006: Message edited by: Paul Clapham ]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Local primitives are always thread safe. Objects are never local, thus can never be assumed thread-safe because of localness.
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Thanks for the response guys,

Just for clarification can you confirm the following:

Local variables are stored on the stack, each thread has a reference to its own set of variables on the stack, so local variables are thread safe.

Objects are stored on the heap and can be accessed by multiple threads so are not thread safe.
How can the object on the heap be accessed by multiple threads unless something like JNDI is used ?

thanks in advance,

James


Objects are never local, thus can never be assumed thread-safe because of localness.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Clarke:
Local variables are stored on the stack, each thread has a reference to its own set of variables on the stack, so local variables are thread safe.


Yes. In fact, each thread has its own stack.


How can the object on the heap be accessed by multiple threads unless something like JNDI is used ?


You can pass references to that object around between different threads.

As long as you don't do that, objects are thread safe, too, of course.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
thanks, much appreciated.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Paul Clapham:
if you assigned it an object reference like this:then no other variable anywhere can contain a reference to that new object. In this case no thread safety issues arise -- until you assign a reference to that object to something outside the scope of the local variable...


Usually true, I agree, but not always. The constructor of Thing is quite at liberty to go find other pre-existing objects and interact with them. Thing's constructor could even pass a reference to itself to another object, so that object could do stuff to the Thing in another thread.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by James Clarke:
Thanks for the response guys,

Just for clarification can you confirm the following:

Local variables are stored on the stack, each thread has a reference to its own set of variables on the stack, so local variables are thread safe.

Objects are stored on the heap and can be accessed by multiple threads so are not thread safe.
How can the object on the heap be accessed by multiple threads unless something like JNDI is used ?

thanks in advance,

James


I would like for you to pay attention to your terms. I think there are some issues there.

You can not start talking about 'local' variables, then shift to talking about objects. This is like baskets vs. apples. Maybe you want to talk about variables that refer to primitives vs. variables that refer to objects?

Do you have c++ background?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Originally posted by Peter Chase:
Usually true, I agree, but not always...
Ah, yes. Another reason to not search out helpful-sounding mantras like "Local variables are always thread-safe", but instead to understand how things work.
[ August 16, 2006: Message edited by: Paul Clapham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variables always thread safe ?