Thread-launched running processes won't destroy (Java)

前端 未结 8 912
清歌不尽
清歌不尽 2021-02-01 08:41

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. He

8条回答
  •  忘掉有多难
    2021-02-01 09:35

    This is not an answer; I am posting complete source for my own attempt at recreating this problem as per discussion in question comments.

    I cannot reproduce this problem on Ubuntu 12.04; OpenJDK 6b_27 (however, see below).

    ProcessTest.java:

    import java.io.*;
    
    public class ProcessTest {
    
        public static final void main (String[] args) throws Exception {
    
            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 e) {
                                System.err.println("exception: " + e.getMessage());
                            } catch(InterruptedException e){
                                System.err.println("exception: " + e.getMessage());
                            }                    
                        }
                    }).start();
            }
    
        }
    
    }
    

    InfiniteLoop.java

    public class InfiniteLoop {
        public static final void main (String[] args) {
            while (true) ;
        }
    }
    

    I cannot reproduce the issue where processes remaining running after the JVM terminates. However, if I add a long delay in the main thread after starting the threads but before returning from main, I do see roughly a dozen running java processes that stick around (although they are terminated when the main program terminates).

    Update:

    I just had it leave about 5 processes running after it terminated. It doesn't always happen. Weird. I want to know more about this too. I have a hunch that it has something to do with destroying the process too quickly or some kind of race condition; maybe java forks something off or does something to create a new process that destroy() doesn't take care of if called too quickly / at the wrong time.

    I found an old bug (but it is not mark resolved) stating that if a process spawns subprocesses they may not be killed by destroy(). bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092 What version of the JDK are you using.

    Here's another reference to what looks like a similar issue: Java tool/method to force-kill a child process And I want to apologize if I've only added confusion to your life, I don't actually use Process that much and am not familiar with the quirks. Hopefully somebody else will step in with a definitive answer. It seems like it doesn't handle subprocesses well, and I'm presuming java forks something off. That's all I got.

提交回复
热议问题