wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Two threads calling the same method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Two threads calling the same method" Watch "Two threads calling the same method" New topic
Author

Two threads calling the same method

Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
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

Joined: Mar 05, 2003
Posts: 32
...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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 05, 2003
Posts: 32
Thanks for the clarification!
// Andreas
 
 
subject: Two threads calling the same method
 
Similar Threads
join() in thread...continues....
J@Whiz question
A doubt on Multiple Threads and the "join()" method !!
Are these two methods equivalent?
Needs clarification about thread execution