Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的进程终止?_Java_Multithreading_Process - Fatal编程技术网

Java 为什么我的进程终止?

Java 为什么我的进程终止?,java,multithreading,process,Java,Multithreading,Process,我有一个可运行的对象,它运行ping操作- Runnable r1 = new Runnable() { @Override public void run() { try{ List<String> commands = new ArrayList<String>(); commands.add("ping");

我有一个可运行的对象,它运行ping操作-

Runnable r1 = new Runnable() {
            @Override
            public void run() {
                try{
                    List<String> commands = new ArrayList<String>();
                    commands.add("ping");
                    commands.add("-c");
                    commands.add("10");
                    commands.add("google.com");

                    System.out.println("Before process");
                    ProcessBuilder builder = new ProcessBuilder(commands);
                    Process process = builder.start();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = null;
                    while ((line=reader.readLine()) != null){
                        System.out.println(line);
                    }
                    process.waitFor();
                    System.out.println("After process");

                }catch (Exception ex){
                   ex.printStackTrace();
                }
            }
        };
我得到这个输出:

Before process
PING google.com (173.194.32.33): 56 data bytes
64 bytes from 173.194.32.33: icmp_seq=0 ttl=53 time=34.857 ms
64 bytes from 173.194.32.33: icmp_seq=1 ttl=53 time=39.550 ms
64 bytes from 173.194.32.33: icmp_seq=2 ttl=53 time=44.212 ms
64 bytes from 173.194.32.33: icmp_seq=3 ttl=53 time=38.111 ms
64 bytes from 173.194.32.33: icmp_seq=4 ttl=53 time=39.622 ms
64 bytes from 173.194.32.33: icmp_seq=5 ttl=53 time=41.391 ms
64 bytes from 173.194.32.33: icmp_seq=6 ttl=53 time=41.280 ms
64 bytes from 173.194.32.33: icmp_seq=7 ttl=53 time=39.645 ms
64 bytes from 173.194.32.33: icmp_seq=8 ttl=53 time=35.931 ms
64 bytes from 173.194.32.33: icmp_seq=9 ttl=53 time=38.245 ms

--- google.com ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 34.857/39.284/44.212/2.575 ms
After process
但如果我在一个新线程中运行它,如下所示:

    Thread thread = new Thread(r1);
    thread.start();
我明白了:

Before process

为什么输出不同?

如果在单独的线程中运行,主线程可能会提前完成,因此您的
r1
将没有足够的时间完成。如果在当前线程中启动,则将等待完成。尝试在启动后添加
thread.join()
,看看发生了什么。

或在启动前添加
thread.setDaemon(false)
。这将防止新线程在主线程终止时停止。
Before process