• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread safe and local variable.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic