简体   繁体   English

如何使用这两个线程以及类A和B的对象创建死锁?

[英]How to create Deadlock using these two threads and objects of classes A and 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");
    }
}

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中的方法无济于事。

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 . 死锁的定义是, A已被锁定,并且需要从锁B同时该B被锁定,并且需要从锁A

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 ar ace 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);
}

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. 您可能还必须删除System.out.println(...)调用,因为它们也已synchronized ,这将更改程序的时间,并可能更难造成死锁。 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". 如果没有输出,要检测没有输出的死锁,可以使用jconsole附加到进程,查看“线程”选项卡,然后单击“检测死锁”。 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. 当2个线程正在旋转时,它应该为〜200%,然后在它们陷入僵局时变为0。

public static void main(String[] args) {
    final Object a = new Object();
    final Object b = new Object();

    Thread t1 = new Thread() {
        @Override
        public void run() {
            synchronized (a) {
                try {
                    sleep(10000);
                } catch (InterruptedException exc) {
                    // 
                }
                synchronized (b) {
                    //
                }
            }
        }
    };

    Thread t2 = new Thread() {
        @Override
        public void run() {
            synchronized (b) {
                try {
                    sleep(10000);
                } catch (InterruptedException exc) {
                    // 
                }
                synchronized (a) {
                    //
                }
            }
        }
    };
    t1.start();
    t2.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM