my dog learned polymorphism
The moose likes Java in General and the fly likes Single Object , Multiple Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Single Object , Multiple Threads" Watch "Single Object , Multiple Threads" New topic

Single Object , Multiple Threads

Natraj Gudla
Ranch Hand

Joined: Feb 09, 2003
Posts: 34
I have a problem,
Suppose there is a class having two methods , MT1,MT2 and MT2 is called from MT1 after some variable initialization like...
MT1 {
//local variable initialization using invoking thread values.
MT2() // call to MT2
here MT1 is not synchronized , where as MT2 is synchronized...
Now I have a single Object of this class , which is used across threads,
It is clear that more than one thread can call MT1 simultaneously.
My question is that
When 2 threads call MT1 simultaneously , what actually happens,
Is it that they will have their own copy of the local variables declared before the call to MT2,
Or is it that one thread will re create the localvariables already created by other thread , and the variables created by first thread no more exist , as MT1 is called for the same object by these threads.
Can any one help me?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Each thread will have its own set of method local variables. For example:

Each time a thread runs method1() then localInt will be initialized to val and localRef will point to a newly created String with contents str. So if thread 1 passes in 2 and "hello" and thread 2 passes in 4 and "world" then you have two different localInts and two different localRefs. Each thread has its own stack to store method locals. As soon as the thread exits the method then all of the method locals are popped off the stack.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link:
subject: Single Object , Multiple Threads
It's not a secret anymore!