【发布时间】: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