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,为什么会死锁呐? 请详细解答 谢谢!!
解决方案 »
- 想做一个学生管理系统,把信息放入一个文件中,实现增删改查,不会修改和删除
- 求个算法
- jtable.print()
- 高手指点,类初始化问题
- java的小问题
- 再发:用java的applet调用delphi封装第三方动态库函数的exe文件出错,急!!!!!分不够再加
- 请大家帮我看看这个程序问题在哪里?
- java.lang.NullPointerException是什么异常,什么情况下发生此异常
- 关于回调的一个小程序 求指教
- 一个关于运行多态的基础问题
- 急:java中如何实现uncode的小字节序的高低位转码?
- 大家没有事情做了 怎么。“ java太简单,学java全是垃圾,天天用别人的API,自己就是个民工 ”这样的破帖子顶来顶去的 ,没有事情吃饱称的啊
当你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();
}
}