Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread safe and local variable.  RSS feed

 
samantha clarkson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All !

i dont know how to make a local variable thread safe .

my local variable (lMyFormatter) is used like below :


Another Thing : i don't think i need that lMyFormatter should be thread safe, because its value doesn't change while using it !!!
if im wrong help me please,

Thanks in advance
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are inherently thread-safe. If your method is called in several threads, each of them will create its own local variables on its own stack, therefore it is not possible for any interference to happen - unless you publish your variable (assign it to an instance or class attribute, or pass it to other objects that do so).

Recommended reading: Java Concurrency in Practice, by Brian Goetz et al.
http://www.javaconcurrencyinpractice.com/
 
samantha clarkson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Istvan Kovacs.

Istvan Kovacs wrote:Local variables are inherently thread-safe. If your method is called in several threads, each of them will create its own local variables on its own stack, therefore it is not possible for any interference to happen - unless you publish your variable (assign it to an instance or class attribute, or pass it to other objects that do so).

Recommended reading: Java Concurrency in Practice, by Brian Goetz et al.
http://www.javaconcurrencyinpractice.com/
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Istvan Kovacs wrote:Local variables are inherently thread-safe.


Be careful here.... If the local variable is a primative variable, then yes, it is thread safe. If the local variable is a reference that is pointing to a locally created object, then yes, it should be thread safe (assuming the statics are thread safe). If the local variable is a reference that is pointing to an externally created object, then it is thread safe, if and only if the object can be used safely in a threaded fashion.

This last clarification is the same clarification as with non-local variables.

Henry
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

Your clarification is perfect, thank you for the addition. It says what my 'unless you publish your variable (assign it to an instance or class attribute, or pass it to other objects that do so)' was meant to say, in a lot more understandable way, and also covers the case when the publishing is done before the local reference is created.

Cheers,
Kofa / István
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!