System.exit是不是线程安全的Linux? [英] System.exit is not thread-safe on Linux?

查看:156
本文介绍了System.exit是不是线程安全的Linux?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从Oracle JDK 1.6,切换到打开JDK 1.7.0_03,我已经打了退出一个相当了不起的僵局:

I've just switched from Oracle JDK 1.6, to Open JDK 1.7.0_03, and I've hit a rather remarkable deadlock on exit:

java.lang.Thread.State: WAITING (on object monitor)
 at java.lang.Object.wait(Native Method)
 at java.lang.Thread.join(Thread.java:1258)
 - locked <0x8608dda0> (a sun.awt.X11.XToolkit$1$1)
 at java.lang.Thread.join(Thread.java:1332)
 at java.lang.ApplicationShutdownHooks.runHooks(ApplicationShutdownHooks.java:106)
 at java.lang.ApplicationShutdownHooks$1.run(ApplicationShutdownHooks.java:46)
 at java.lang.Shutdown.runHooks(Shutdown.java:123)
 at java.lang.Shutdown.sequence(Shutdown.java:167)
 at java.lang.Shutdown.exit(Shutdown.java:212)
 - locked <0x8603df28> (a java.lang.Class for java.lang.Shutdown)
 at java.lang.Runtime.exit(Runtime.java:107)
 at java.lang.System.exit(System.java:960)

看来,你必须从AWT事件队列调用System.exit。这是真的吗?有没有在太阳文档一个线程需要的 Runtime.exit

我已经打哪里得到AWT树锁,只需要在Linux上其他令人吃惊的情况,但这个叫绝。这是一个bug,或者有我只是错过了文档中的东西吗?

I've hit other surprising cases where getting the AWT tree lock is required only on Linux, but this one takes the cake. Is this a bug, or have I just missed something in the documentation?

推荐答案

这取决于,在 runHooks 方法将开始通过注册的勾线运行.addShutdownHook 并等待它们完成。如果您的勾线是锁定一些AWT事件线程需要的资源太多,很可能会引起死锁。

It depends, the runHooks method will start any hook threads registered through Runtime.addShutdownHook and wait for them to be finished. If any of your hook threads is locking some resources that the AWT event thread is requiring too, they may cause dead lock.

如果您需要调用System.exit在AWT事件线程,我建议你把它像另一个线程:

If you have to call System.exit in your AWT event thread,I suggest you call it in another thread like:

 new Thread(){
            public void run() {
                System.exit(0);
            }
   }.start();

这篇关于System.exit是不是线程安全的Linux?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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