class A{
public B b;
synchronized void methoda(){
String name = Thread.currentThread().getName();
System.out.println(name + " entered class A.methoda().");
try{
Thread.sleep(1000);
}catch(InterruptedException ie)
{
}
System.out.println(name + " trying to call B.methodb().");
b.methodb();
}
synchronized void methodb(){
System.out.println("Inside A.methodb().");
}
}class B{
public A a;
synchronized void methoda(){
String name = Thread.currentThread().getName();
System.out.println(name + " entered class B.methoda().");
try{
Thread.sleep(1000);
}catch(InterruptedException ie)
{
}
System.out.println(name + " trying to call A.methodb().");
a.methodb();
}
synchronized void methodb(){
System.out.println("Inside B.methodb().");
}
}class DeadLock implements Runnable{
A a;
B b;
DeadLock(){
a = new A();
b = new B();
a.b = b;
b.a = a;
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.methoda();
System.out.println("Over!");
}
public void run(){
Thread.currentThread().setName("RacingThread");
b.methoda();
System.out.println("Back from RacingThread.");
}
public static void main(String[] args){
new DeadLock();
}
}为什么会出现死锁?2个线程分别调用methoda,然后交叉调用methodb,为什么会死锁呐? 请详细解答 谢谢!!
public B b;
synchronized void methoda(){
String name = Thread.currentThread().getName();
System.out.println(name + " entered class A.methoda().");
try{
Thread.sleep(1000);
}catch(InterruptedException ie)
{
}
System.out.println(name + " trying to call B.methodb().");
b.methodb();
}
synchronized void methodb(){
System.out.println("Inside A.methodb().");
}
}class B{
public A a;
synchronized void methoda(){
String name = Thread.currentThread().getName();
System.out.println(name + " entered class B.methoda().");
try{
Thread.sleep(1000);
}catch(InterruptedException ie)
{
}
System.out.println(name + " trying to call A.methodb().");
a.methodb();
}
synchronized void methodb(){
System.out.println("Inside B.methodb().");
}
}class DeadLock implements Runnable{
A a;
B b;
DeadLock(){
a = new A();
b = new B();
a.b = b;
b.a = a;
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.methoda();
System.out.println("Over!");
}
public void run(){
Thread.currentThread().setName("RacingThread");
b.methoda();
System.out.println("Back from RacingThread.");
}
public static void main(String[] args){
new DeadLock();
}
}为什么会出现死锁?2个线程分别调用methoda,然后交叉调用methodb,为什么会死锁呐? 请详细解答 谢谢!!
当你b.methoda的时候b.methoddb也被lock当a.methoda结束等待之后去调用b.methoddb的时候 b.methoddb是被锁住的(b.methoda执行中)
所以a等待。当b.methoda结束等待之后去掉用a.methodb的时候a.methoda还在执行结束(a.methodb被锁)
所以b等待然后大家都一起等死了下面是我的测试代码class B{synchronized void methoda(){
System.out.println( " entered class B.methoda().");
try{
System.out.println("waitting for invoke methoda......(5s)");
Thread.sleep(5000);
}catch(InterruptedException ie)
{
}
System.out.println( " outed class B.methoda()");
}synchronized void methodb(){
System.out.println("Inside B.methodb().");
}
}public class DeadLock implements Runnable{
B b;DeadLock(){
b = new B();
Thread.currentThread().setName("MainThread");
new Thread(this).start();
try{
System.out.println("waitting for invoke methodb......(1s)");
Thread.sleep(1000);
}catch(InterruptedException ie)
{
}
b.methodb();
System.out.println("Over!");
}public void run(){
b.methoda();
}public static void main(String[] args){
new DeadLock();
}
}