• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on thread access same method

 
Steven Jiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Anyone can help me with the question, or could you please direct me to the right resource.

I have a method: MethodA(){...}, which is accessed by multi-thread.

How each thread execute the method? Each thread create seperate memory space to hold the method related variable and ....?

Thanks a lot.

Steven
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread has a separate stack. No other thread can see that stack. Therefore, all data that is on the stack is thread-safe. That is all method parameters and local variables.

Be careful, though, when thinking about object references. Many variables and parameters in Java are object references. If the object reference is on the stack, it is thread-safe. But the object to which it points is not on the stack: it's on the heap. There's only one heap, so objects on the heap are not thread-safe unless they are specifically coded to be so.
[ April 03, 2008: Message edited by: Peter Chase ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!