Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Are Local Variables Thread Safe  RSS feed

 
Puja Agarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two threads executing same method will have their separate copy of the local variables.
 
Henry Wong
author
Sheriff
Posts: 22841
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!