In that case if both the threads try to access
TestStaticSync.testSync1() , Then the static synchronized methods of the same class should block each other as only one lock per class.
public class ThreadOne implements Runnable
{
TestStaticSync testStaticSync;
ThreadOne(TestStaticSync t)
{
this.testStaticSync = t;
new
Thread(this,"ThreadOne").start();
}
public void run()
{
TestStaticSync.testSync1();
testStaticSync.nonStaticMethod1();
}
}
public class ThreadTwo implements Runnable
{
TestStaticSync testStaticSync;
ThreadTwo(TestStaticSync t)
{
this.testStaticSync = t;
new Thread(this,"ThreadTwo").start();
}
public void run()
{
TestStaticSync.testSync1();
testStaticSync.nonStaticMethod2();
}
}
But, in that case also I'm getting the same output.
Inside testSync1 ::::counter:::::1
Inside testSync1 ::::counter:::::2
Inside nonStaticMethod1 ::::counter:::::3::::count:::::1
Inside nonStaticMethod2 ::::counter:::::4::::count:::::2