如何使用这两个线程和类A和B的对象创建死锁? [英] How to create Deadlock using these two threads and objects of classes A and B?

查看:275
本文介绍了如何使用这两个线程和类A和B的对象创建死锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class DeadLock {

    public static void main(String[] args) {

        final A a = new A();
        final B b = new B();

        new Thread(new Runnable(){
            @Override
            public void run() {
                 a.aMethod(b);
                }
        },"Thread-2").start();

        new Thread(new Runnable(){
            @Override
            public void run() {
                b.bMethod(a);
            }
        },"Thread-2").start();
    }
}

class A {
    public  void aMethod(B b) {
        System.out.println("A method");
    }
}

class B {
    public  void bMethod(A a) {
        System.out.println("B method");
    }
}

据我所知,当两个或多个线程出现死锁时阻止等待对方。如何使用上面的代码实现相同的功能?同步A和B类中的方法没有帮助。

I understand that Deadlock occurs when two or more threads are blocked waiting for each other. How do I implement the same using the code above? Synchronizing the methods in classes A and B doesn't help.

推荐答案


如何使用上面的代码实现相同的功能?同步A和B类中的方法没有帮助。

How do I implement the same using the code above? Synchronizing the methods in classes A and B doesn't help.

死锁的定义是 A 已被锁定,需要锁定来自 B ,同时 B 已被锁定且需要锁定来自 A

The definition of deadlock is that A is locked and needs the lock from B at the same time that B is locked and needs the lock from A.

您无法通过单个线程调用来模拟它,因为可能第一个启动的线程将在第二个线程启动之前完成。这是 ace条件,其中线程正在陷入僵局。

You aren't going to be able to simulate it with a single thread call because likely the first thread that is started will finish before the second thread starts. This is a race condition where the threads are racing to deadlock or not.

你需要在两个线程中循环并反复尝试双重锁定。像下面这样的东西应该工作。在某些时候,你会看到输出停止。

You need to loop in both threads and try the dual lock over and over. Something like the following should work. At some point you will see the output stop.

public void run() {
   while (true) {
     a.aMethod(b);
   }
}
...
public void run() {
   while (true) {
     b.bMethod(a);
   }
}
...
public synchronized void aMethod(B b) {
    System.out.println("B method");
    b.bMethod(this);
}
...
public synchronized void aMethod(A a) {
    System.out.println("A method");
    a.aMethod(this);
}

您可能还需要删除 System.out .println(...)调用,因为它们也是 synchronized ,这将改变程序的时间并可能使其更难以解决死锁。如果没有输出,要在没有输出的情况下检测死锁,可以使用jconsole将其附加到进程,请查看线程选项卡,然后单击检测死锁。您还可以观看程序的负载。当2个线程正在旋转时它应该是~200%,然后在它们被锁定时变为0。

You may also have to remove the System.out.println(...) calls because they also are synchronized which will change the timing of your program and may make it harder to hit a deadlock. Without the output, to detect the deadlock without the output you can attach to the process using jconsole, look at the Threads tab, and click "Detect Deadlock". You can also watch the load of your program. It should be ~200% while 2 threads are spinning and then go to 0 when they are deadlocked.

这篇关于如何使用这两个线程和类A和B的对象创建死锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆