我感到困惑與以下情形所有的時間Java的同步,並通過多線程使用靜態同步方法
只有一個MyClass的對象,並有兩個線程T1,T2。 現在有一個線程說T2將能夠使用具有唯一MyClass對象鎖定的同步方法m1(),並且如果它嘗試訪問m1(),則其他線程T2將被阻塞。
現在我的感覺是,如果T2將嘗試通過訪問靜態共享字段訪問靜態同步方法m2(),它將被阻塞,因爲當前對象鎖定與T1並且將不能夠執行m2(),如果有Myclass然後T2線程的兩個對象將能夠訪問m1()。我正確或錯誤?
class MyClass
{
public static int i = 5;
public synchronized void m1()
{
System.out.println(i); //uses static field i of MyClass
//T1 is executing this method
}
public static synchronized void m3()
{
//T2 will be able to call this method on same object lock while it is using
//static field i???
System.out.println(i);//uses static field i of MyClass
}
}
這很混亂請幫忙。提前致謝。