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

Two sync methods of same class called by two threads  RSS feed

 
ani jadhao
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I want to clear a doubt about threads .

In a class A there are two separate instance synchronized methods M1 and M2. THere are two threads running , T1 and T2 .

Suppose T1,T2 accessing T1-->M1 and T2-->M2 at the same time .

Can T1,T2 access M1(called by T1) and M2(called by T2) of A's instance at one time ?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have two separate instances of A and the methods aren't static , then they have separate locks and the threads can acquire them, ie one thread can acquire the lock on the first instance whilst the second thread holds a lock on a separate instance.

So the answer is yes if I'm reading your question correctly ;-)
 
ani jadhao
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is only one instance of A .

but there are two MEMBER (instance )methods in class A.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instane methods are synchronized on the this object, so if both threads call methods that share the same this reference those methods can not be run concurrently. If the methods are called on different instances then they can be run concurrently.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!