System.out.format如何防止死锁? [英] How can System.out.format prevent a deadlock?

查看:121
本文介绍了System.out.format如何防止死锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在经典 Java死锁教程将防止发生死锁,我无法弄清楚原因。

I've found that including a call to System.out.format in the classic Java Deadlock Tutorial will prevent deadlock from occurring, and I can't figure out why.

下面的代码与下面的代码相同教程,加上 main System.out.format(我是%s ...没有死锁给你!\\ \\ n \ n,alphonse.getName());

The code below is the same as that of the tutorial, with the addition to main of System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

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) throws InterruptedException {
        final Friend alphonse = new Friend("Alphonse");
        final Friend gaston = new Friend("Gaston");

        System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

        new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }).start();

        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }).start();
    }
}

这是输出:

Hi, I'm Alphonse...no deadlock for you!

Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed back to me!
Gaston: Alphonse has bowed to me!
Alphonse: Gaston has bowed back to me!

删除违规行会导致通常的死锁:

Removing the offending line results in the usual deadlock:

Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed to me!
... deadlock ...

以某种方式调用System.out.format改变线程获取对象内在锁的方式?

Is the call to System.out.format somehow changing the way the threads acquire the intrinsic locks on the objects?

更新:

我能得到系统只是通过改变我在代码中启动线程的位置来再次死锁:

I was able to get the system to deadlock again just by changing where I start the threads in the code:

public static void main(String[] args) throws InterruptedException {
    final Friend alphonse = new Friend("Alphonse");
    final Friend gaston = new Friend("Gaston");

    System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

    Thread t1 = new Thread(new Runnable() {
        public void run() { alphonse.bow(gaston); }
    });

    Thread t2 = new Thread(new Runnable() {
        public void run() { gaston.bow(alphonse); }
    });

    t1.start();
    t2.start();
}

这引出了一个问题,即我们如何更深入地了解线程调度程序行为,但我将保存不同的一天。

This begs the question about how we can get greater insight into how the thread scheduler behaves, but I'll save that for a different day.

推荐答案

你没有真正删除死锁而是(因为一些内部JVM原因)改变了线程的时间,以便其中一个线程在之前进入 bowBack() 其他电话 bow()
只需输入 bow sleep(1000),您的死锁就会重新出现。

You did not really remove the deadlock but rather (because of some internal JVM reason) changed that timing of the threads so that one of the threads enters bowBack() before the other calls bow(). Just put in bow: sleep(1000) and your deadlock will reappear.

请注意,只有当线程处于幸运时序时,死锁才会始终发生。在这种情况下,当两个线程都进入 bow 并且在它们中的任何一个调用 bowBack

Note that deadlock does not always happen, only when the threads are in lucky timing. In this case, deadlock will happen when both threads enter bow and before any of them call bowBack

...
一些内部JVM原因可以是以下内容:

... And "some internal JVM reason" can be the following:

在您的情况下,实际上有三个线程:执行 main t1 t2 的线程。
print 隐藏死锁的原因可能是线程调度程序决定 main 仍有工作要做,即刷新io缓冲区,因此在启动 t1 之后和启动t2之前让 main 继续。如果你使用的是双核cpu,只能运行 main t1 ,但 t2 会等待,因为 print 是一个缓慢的操作。上下文切换需要一些时间, t1 将在 t2 启动之前完成...因此不会发生死锁。但这并不意味着如果你再次运行程序就不会发生死锁。

In your case there are actually three threads: the one executing main, t1, and t2. The reason why putting print hides the deadlock can be that the thread scheduler decided that main still had work to do, i.e. flushing the io buffers, and therefore let main continue after starting t1 and before starting t2. If you are on a dual-core cpu, only main and t1 would run, but t2 would wait, since print is a slow operation. Context switching would take some more time, and t1 would finish before t2 could start...so no deadlock would happen. But that does not mean that if you run the program again deadlock won't happen.

如果你想玩,可以创建一个队列并在该队列中推送令牌(线程名称),然后加入你的线程在main中。完成后,打印队列内容,您可以观察线程的时间。

If you want to play, create a queue and push tokens (thread names) in that queue, then join your threads in main. After they finish, print the queue content and you can observe the timing of the threads.

这篇关于System.out.format如何防止死锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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