本帖最后由 细水长流 于 2012-7-18 22:01 编辑
毕老师java基础教学视频里有一段演示 死锁的代码,代码如下
这段代码自己检查了好几遍,没有错误,编译也通过了,运行的时候如下提示
Exception in thread "main" java.lang.NoSuchMethodError: Test.<init>(Z)V
at DeadLockTest.main(DeadLockTest.java:48)
跪求求解答
/*
死锁。
同步中嵌套同步
*/
class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
synchronized(MyLock.locka)
{
System.out.println("if locka");
synchronized(MyLock.lockb)
{
System.out.println("if lockb");
}
}
}
else
{
synchronized(MyLock.lockb)
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
}
}
}
}
class MyLock
{
static Object locka = new Object();
static Object lockb = new Object();
}
class DeadLockTest
{
public static void main(String[] args)
{
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
|
|