Suppose I have a class where in the have methods synchronized in the way as follows.
public class Test{
public static synchronized void method1(){
}
public static synchronized void method2(){
}
public synchronized void method3(){
}
public synchronized void method4(){
}
}
So there is a scenario when two threads are calling method1
and method2
simultaneously.I feel that only one of the methods be allowed to call.What will be the case if they call method1
and method3
.Will there be a same scenario here too?what will be the case with method3
and method4
from same object?
If you have two objects of class Test, referenced by x and y, there will be three monitors, one for Test, one for x, and one for y.
method1 and method2 both use the Test monitor, and so exclude each other. method3 and method4 each use the monitor for their target object, so they exclude each other if called for the same object, but not if called for different objects. They don't involve the Test monitor, so they don't exclude the static methods.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments