Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two threads calling the same method

 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I would like to have some clarification on on basic java thread issue and hope that someone can help me out.
If there are two differnet threads calling a method of one single java instance I understand that there can be syncronization issues (they are writing over local variables for each other).
My question is if the parameters sent in to the method as well gets overritten or if they are per thread.
Example:

One thread calls the method with parameter "me" and the other one with the parameter "you" (they call the method at the same time). I understand that the printout after "Print 1" can be "me" or "you" twice instead of one "me" and one "you". But what is the case with the Print 2? Is it the same behavior here or will it always write one �"me" and one "you"?
Thanks in advance!
// Andreas
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...sorry, forgot this question as well...
Is it so that ALL variables within the method is protected and there is only class global variables that can change in this case?
// Andreas
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread gets its own copies of local variables. So they don't have the problems that instance and class (=static) variables have. But remember that object variables are in fact references -- so if a local variables refers to an object that is shared with other threads, that object has to be threadsafe.
- Peter
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification!
// Andreas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic