Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Method variables & Threads  RSS feed

 
kyle novak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the following class...
public class A {
int m_i = 10;
public void method1() {
int x = 0;
x = 10;
}
}
Does method1() need to be synchronized even though it does not access any class instance variables? Asked another way: Do 2 or more threads accessing the same un-synchronized method (method1() in this case) step on eachother's toes, even if the method only uses variables local to that method? I hope I've explained it well.
Thanks,
Kyle
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you don't have to synchronized the method. Every local variables within the method are local, and during execution they have their own memory space. So different threads execution have their own stack of local variables.
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As best I understand it, you would not need to synchronize this. Each invocation of method1() will have its own frame on the stack, which x will be contained/scoped by. However, if you were changing m_i even once in the method, you would of course need to synchronize.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!