Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Thread safe and local variable.

 
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
 
Ranch Hand
Posts: 111
1
  • 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/

 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 111
1
  • 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
 
Fire me boy! Cool, soothing, shameless self promotion:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic