This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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: 18550
    
  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
 
Similar Threads
request atributes are always thread-safe?
Thread safety
Help w/SingleThreadModel
thread
Threads and local variables