I have tried this thing by writing the following program. The output of this program is showing that both the threads are taking turns. One thread(t1) is executing static method of the class and other(t2) is executing an instance method of object of the same class, so If they can take turns it means class level lock is independent of obejct level lock, aint?
public class javaranch {
public static void main(
String[] args){
Thread t1 = new Thread(new th1());
t1.start();
Thread t2 = new Thread(new th2());
t2.start();
}
}
class th1 implements Runnable{
public void run(){
common.classMethod();
}
}
class th2 implements Runnable{
public void run(){
common commonObject = new common();
commonObject.iMethod();
}
}
class common{
public static synchronized void classMethod(){
while(true){
System.out.println(Thread.currentThread().getName() + " In static sync method");
}
}
public void iMethod(){
while(true){
System.out.println(Thread.currentThread().getName() + " In instance sync method");
}
}
}