This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are Local Variables Thread Safe

 
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
Marshal
Pie
Posts: 22113
88
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