This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Are Local Variables Thread Safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Are Local Variables Thread Safe" Watch "Are Local Variables Thread Safe" New topic
Author

Are Local Variables Thread Safe

Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
Hi,

Are Local Variables Thread Safe , even if the method is not synchronized.

Regards
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Two threads executing same method will have their separate copy of the local variables.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Originally posted by amitabh mehra:
Two threads executing same method will have their separate copy of the local variables.


Yes, but it is possible for two local variables (references) to point to the same objects -- so it is not guaranteed to be thread safe.

Add the synchronized keyword to the method doesn't guaranteed threadsafe either -- as you need to have a synchronized object that is used by all accesses (that are not threadsafe).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Local Variables Thread Safe