【问题标题】:Why does this program stops excecuting为什么这个程序停止执行
【发布时间】:2015-03-09 16:35:56
【问题描述】:

我真的是线程、进程和终端的新手。 到目前为止,它还没有那么糟糕,但我被困住了。

在我的 chiplotleErrorStream 中它永远不会到达:

它只打印它找不到绘图仪的所有错误(我没有连接电缆)。但它永远不会:

System.out.println("it never reaches or prints this");

我不明白为什么。 我做错了什么?

import java.io.*;

public class ExecTest {

    ChiplotleInputStream chiplotleInputStream;
    ChiplotleErrorStream chiplotleErrorStream;

    PrintWriter pw;

    public static void main(String[] args) {

        new ExecTest().setup();

    }


    // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


    public void setup() {

        try {
            Runtime runTime = Runtime.getRuntime();

            Process process = runTime.exec("chiplotle");

            chiplotleInputStream = new ChiplotleInputStream(process);
            chiplotleErrorStream = new ChiplotleErrorStream(process);

            OutputStream os = process.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            pw = new PrintWriter(bw);


            new Thread(chiplotleInputStream).start();
            new Thread(chiplotleErrorStream).start();

//            process.destroy();

        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }


    // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


    public class ChiplotleInputStream implements Runnable {

        BufferedReader in;

        public ChiplotleInputStream(Process process) {
            in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        }

        @Override
        public void run() {

            while (true) {

                String line;

                try {
                    while ((line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    Thread.sleep(100);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
    }


     // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


     public class ChiplotleErrorStream implements Runnable {

         BufferedReader in;

         public ChiplotleErrorStream(Process process) {
             in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
         }

         @Override
         public void run() {

             while (true) {

                 String line;

                 try {
                     while ((line = in.readLine()) != null) {
                         System.out.println(line);
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }

                 System.out.println("it never reaches or prints this");
                 // hoping for errors in the terminal so we see something
                 // but if it doesn't come here...
                 pw.println("dfsfsdfsdf");

                 try {
                     Thread.sleep(100);

                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }


         }
     }

    // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


}

【问题讨论】:

  • 发布您的跟踪错误
  • 发布您遇到的 full 错误。
  • 您能否验证您创建的流程没有任何问题?

标签: java multithreading terminal


【解决方案1】:

您正在运行的程序在输出任何内容之前需要输入。

所以你创建的进程就坐在那里,等待你向你创建的PrintWriter写命令。

只要进程处于活动状态并等待,它就不会向其输出流(您的进程输入流)或错误流输出任何内容。

因此,您在这些流上创建的阅读器上的 readLine() 方法只是被阻止,等待一些输入通过。您标记的行只会在chiplotle 应用程序退出时执行,此时它将关闭其流,readLine() 将获得null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2020-10-04
    • 2020-08-31
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多