aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 19060
    
  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