这个java代码如何产生死锁

这个java代码如何产生死锁

本文介绍了这个java代码如何产生死锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过oracle docs for deadlock ..
i找到了这段代码

i am going through oracle docs for deadlock..i found this code

public class Deadlock {
    static class Friend {
        private final String name;
        public Friend(String name) {
            this.name = name;
        }
        public String getName() {
            return this.name;
        }
        public synchronized void bow(Friend bower) {
            System.out.format("%s: %s"
                + "  has bowed to me!%n",
                this.name, bower.getName());
            bower.bowBack(this);
        }
        public synchronized void bowBack(Friend bower) {
            System.out.format("%s: %s"
                + " has bowed back to me!%n",
                this.name, bower.getName());
        }
    }

    public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }).start();
        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }).start();
    }
}

我无法理解,在什么情况下死锁会发生了?

I am failing to understand, in what case deadlock will happen?

我运行此代码,它运行正常。所以必须有一些特殊事件,当发生死锁时?

I run this code, it works fine. So there must be some special event, when deadlock will happen?

假设首先在 alphonse 对象上调用bow,当bower对象上调用 bower.bowBack(this)时,它会锁定 alphonse 对象吗?
因为如果它保持锁定, bow 函数在另一个对象上将无法锁定,直到 alphonse 离开它锁,它永远不会是一个死锁情况..

Suppose bow is called on alphonse object first, will it leave its lock on alphonse object when bower.bowBack(this) is called on bower object?Because if it retains its lock, bow function on another object will not get lock until alphonse leave its lock, and it will never be a deadlock situation..

推荐答案

如果在打印后放入Thread.sleep(1000)第一行,在调用bowBack之前,你应该看到一个死锁。无论如何都会发生这种僵局,这种情况很少发生。

If you put a Thread.sleep(1000) after printing the first line and before making the call to bowBack, you should see a deadlock. This deadlock can happen anyway, it's will be rare.

你有两个线程和两个被获取的锁是不同的命令。这可以使每个线程保持一个锁但无法获得第二个锁。即死锁。

You have two threads and two locks being acquired is different orders. This can leave each thread holding one lock but unable to get the second lock. i.e. a deadlock.

注意:线程需要花费大量时间才能启动,这意味着第一个线程可以在第二个线程开始之前运行完成,因此您不太可能看到问题。

Note: threads take a lot of time to start which means the first thread can run to completion before the second one starts, so it is unlikely you will see the problem.

这是一款益智游戏。这会造成死锁,你能看出原因吗?

Here is a puzzler for you. This creates a deadlock, can you see why?

class A {
    static final int i;
    static {
        i = 128;

        Thread t = new Thread() {
            public void run() {
                System.out.println("i=" + i);
            }
        };
        t.start();
        try {
           t.join();
        } catch (InterruptedException e) {
           Thread.currentThread().interrupt();
        }
    }

这篇关于这个java代码如何产生死锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:24