• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread sharing local variables?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method is defined in a class that implements Thread.

If a method has local variables, and is called twice,
will the 2 invocations share the copies of the local variables? or will the local variables have their own values for each invocation of the method.

(I have an understanding that they will have their own local space)
When then will synchronization be needed?

Thanks in advance.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the object bob is created everytime the method is run

The int someValue will change when method() is run on each instance of SomethingElse - method should probably be synchronized to prevent things changing the value of someValue while the other instances are using it.

Hope that helps
 
Avianu Sud
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom, Thanks.
The example you state makes sense.

I am also asking the case where someMethod has a input parameter, and local variables. Do those need to be Threadsafe? or does each call to someMethod, has its own memory space for the local variables?


- Avi
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread entring the method uses the parameter objects sent to it. If two callers send references to the same object then the two threads could get into race conditions modifying fields on the object. If the callers send different parameter objects they are safe. Almost. The callers are starting new threads and we have no idea what else they might try to do with the objects they passed as parameters. Doesn't make us feel very warm and fuzzy does it?
 
    Bookmark Topic Watch Topic
  • New Topic