本帖最后由 今天是坏蛋 于 2013-10-30 10:20 编辑
看了毕老师的死锁例子,自己学着写了一个,高手看看对不对。
还有这个我不是很懂
class DeadLock
{
static Object lock_A=new Object();
static Object lock_B=new Object();
}
不是说源文件名是*.class的吗,锁怎么是 DeadLock.lock_B ?
class TestLock implements Runnable
{
private int a=1;
public int test(int a)
{
this.a=a;
return a;
}
public void run()
{
if (a==0)
{
synchronized(DeadLock.lock_A)
{
System.out.println(Thread.currentThread().getName()+" lock one A...");
synchronized(DeadLock.lock_B)
{
System.out.println(Thread.currentThread().getName()+" lock one B...");
}
}
}
else
{
synchronized(DeadLock.lock_B)
{
System.out.println(Thread.currentThread().getName()+" lock tow B...");
synchronized(DeadLock.lock_A)
{
System.out.println(Thread.currentThread().getName()+" lock two A...");
}
}
}
}
}
class DeadLock
{
static Object lock_A=new Object();
static Object lock_B=new Object();
}
class A
{
public static void main(String []args)
{
TestLock a0 = new TestLock();
a0.test(0);
TestLock a1 = new TestLock();
Thread t0 = new Thread(a0);
Thread t1 = new Thread(a1);
t0.start();
t1.start();
}
}
|
|