是什么会导致Java在System.exit()之后继续运行? [英] What can cause Java to keep running after System.exit()?

查看:328
本文介绍了是什么会导致Java在System.exit()之后继续运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,正在通过ProcessBuilder从另一个Java程序启动. System.exit(0)是从子程序中调用的,但是对于我们的某些用户(在Windows上),与该子级关联的java.exe进程不会终止.子程序没有关机钩子,也没有SecurityManager,它可能会阻止System.exit()终止VM.我自己无法在Linux或Windows Vista上重现该问题.到目前为止,唯一的问题报告来自两个Windows XP用户和一个Vista用户,他们使用两个不同的JRE(1.6.0_15和1.6.0_18),但是他们每次都能重现该问题.

I have a Java program which is being started via ProcessBuilder from another Java program. System.exit(0) is called from the child program, but for some of our users (on Windows) the java.exe process associated with the child doesn't terminate. The child program has no shutdown hooks, nor does it have a SecurityManager which might stop System.exit() from terminating the VM. I can't reproduce the problem myself on Linux or Windows Vista. So far, the only reports of the problem come from two Windows XP users and one Vista user, using two different JREs (1.6.0_15 and 1.6.0_18), but they're able to reproduce the problem every time.

有人能提出为什么System.exit()之后JVM仅在某些机器上无法终止的原因吗?

Can anyone suggest reasons why the JVM would fail to terminate after System.exit(), and then only on some machines?

我让用户安装了JDK,因此我们可以从有问题的VM中进行线程转储.用户告诉我的是,只要单击菜单中的"Quit"项,VM进程就会从VisualVM中消失-但是,根据Windows Task Manager的说法,该进程并没有终止,无论它持续了多长时间用户等待(分钟,小时),它永远不会终止.

Edit 1: I got the user to install the JDK so we could get a thread dump from the offending VM. What the user told me is that the VM process disappears from VisualVM as soon as he clicks on the 'Quit' item in my menu---but, according to Windows Task Manager, the process hasn't terminated, and no matter how long the user waits (minutes, hours), it never terminates.

我现在已经确认,父程序中的Process.waitFor()永远不会为有问题的至少一个用户返回.因此,总结一下:子VM似乎已经死了(VisualVM甚至看不到它),但是父VM仍然认为该进程处于活动状态,Windows也是如此.

Edit 2: I have confirmed now that Process.waitFor() in the parent program never returns for at least one of the users having the problem. So, to summarize: The child VM seems to be dead (VisualVM doesn't even see it) but the parent still sees the process as live and so does Windows.

推荐答案

父进程只有一个线程 致力于消费每个 孩子的STDOUT和STDERR(其中 将输出传递到日志 文件).据我所知,这些是 工作正常,因为我们看到了 我们希望在中看到的所有输出 日志

The parent process has one thread dedicated to consuming each of the child's STDOUT and STDERR (which passes that output through to a log file). So far as I can see, those are working properly, since we're seeing all the output we expect to see in the log

当我使用stdout/stderr时,我的程序没有从任务mgr中消失时,我遇到了类似的问题.就我而言,如果我在调用system.exit()之前关闭了正在侦听的流,则javaw.exe会挂在身边.奇怪,这不是写到流中...

i had a similar problem with my program not disappearing from task mgr when i was consuming the stdout/stderr. in my case, if I closed the stream that was listening before calling system.exit() then the javaw.exe hung around. strange, it wasn't writing to the stream...

在我的情况下,解决方案是简单地刷新流而不是在存在之前将其关闭.当然,您总是可以刷新,然后在退出前重定向回stdout和stderr.

the solution in my case was to simply flush the stream rather than close it before existing. of course, you could always flush and then redirect back to stdout and stderr before exit.

这篇关于是什么会导致Java在System.exit()之后继续运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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