线程启动的运行进程不会破坏(Java) [英] Thread-launched running processes won't destroy (Java)

查看:138
本文介绍了线程启动的运行进程不会破坏(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动多个线程并让每个exec()然后destroy()一个正在运行的java进程导致某些进程没有被销毁并且在程序退出后仍然运行。这是一些重现问题的代码。我注意到你开始的线程越多,进程就越活跃。而且,在destroy()之前睡眠越多,进程就越少。 (我使用InfiniteLoop作为示例。任何正在运行的进程都可以解决问题。)

Starting multiple threads and having each exec() then destroy() a running java process result in some of the process not being destroyed and still running after program exit. Here is some code that reproduce the issue. I noticed the more threads you start, the more processes stay alive. And the more sleep before destroy(), the less processes stay alive. (I used InfiniteLoop as an example. Any running process will do the trick.)

编辑:已向Oracle报告错误,等待答案。随意分享关于该主题的任何知识/实验。

EDIT : Bug has been reported to Oracle, waiting for an answer. Feel free to share any knowledge/experiments on the subject.

for(int i = 0; i < 100; i++)
{
  new Thread(new Runnable()
  {
    public void run()
    {
      try
      {
        Process p = Runtime.getRuntime().exec(new String[]{"java", "InfiniteLoop"});
        Thread.sleep(1);
        p.destroy();
      }catch(IOException | InterruptedException e){e.printStackTrace();}                    
    }
  }).start();
}


推荐答案

使用 p.waitFor(); p.destroy(); 之前,

这将确保完成前一个过程。我认为p.destroy命令比 exec()命令执行操作更早被调用。因此它变得无用。

this will ensure the completion of the previous process. I think you p.destroy command gets invoked sooner than the exec() command performs the action. Therefore it becomes useless.

这篇关于线程启动的运行进程不会破坏(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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