As this SO answer exactly points
there is no link between synchronized static methods and sync'ed non static methods in
this context
To prove this.
public class SimpleClassTest {
Static Sync method
public static synchronized void X1() {
System.out.println("Before X1 Exec.. From"+Thread.currentThread().getName());
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After X1 Exec.. From"+Thread.currentThread().getName());
}
Non Static Sync Method
public synchronized void X2() {
System.out.println(" X2 Exec.. From"+Thread.currentThread().getName());
}
First method simply sleep for some time and wake up.
Second method sync but not static .
Now try to create two threads and call this method same time ( approx)
public static void main(String args[]){
final SimpleClassTest instance = new SimpleClassTest();
First thread for static
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
SimpleClassTest.X1();
}
},"Thread1");
Second thread for non static
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
instance.X2();
}
},"Thread2");
Now start both the threads
t1.start();
t2.start();
And result
Before X1 Exec.. FromThread1
X2 Exec.. FromThread2
After X1 Exec.. FromThread1
From the result both Threads running without locking each other. Because if the first thread locks the same object on which second thread locking then thread 2 would wait until the first thread completes .
So from this we are sure both are running in parallel.
Firs thread has lock on SimpleClasstest.class since it is static method nothing to do with instance.
Second thread has lock on instance since it is non static.
Hope it clears