【发布时间】:2013-03-06 14:39:48
【问题描述】:
如果静态同步方法和实例同步方法试图在不同线程中访问同一类的静态字段,以下程序的行为会是什么?任何线程都会被阻塞吗?非常混乱。
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
}
}
【问题讨论】:
标签: java synchronization