File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Object , Multiple Threads

 
Natraj Gudla
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic