等!!!
空白已存在 2017-08-21 08:37:26 package com.wjk.java.synchronized_;
public class Deadlock {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestA a = new TestA();
TestB b = new TestB();
Thread t1 = new Thread(a,"线程A");
Thread t2 = new Thread(b,"线程B");
t1.start();
t2.start();
}
}
class TestA implements Runnable{
public void run(){
A();
}
public synchronized void A(){
TestB b = new TestB();
System.out.println(Thread.currentThread().getName());
//此处等待是给B能锁住机会
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//占用着A,调用B
b.B();
System.out.println("这行代码执行不了,因为死锁了");
}
}
class TestB implements Runnable{
public void run(){
B();
}
public synchronized void B(){
TestA a = new TestA();
System.out.println(Thread.currentThread().getName());
//此处等待是给A能锁住机会
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//占用着B,调用A
a.A();
System.out.println("这行代码执行不了,因为死锁了");
}
}
为什么没有发生死锁,而是递归栈溢出了?