• 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: 21021
78
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic