无法读取我使用runtime.exec运行的子进程的系统输出

时间:2018-10-15 21:49:35

标签: java concurrency inputstream outputstream

我正在尝试创建一个子流程,向其发送两个整数,然后从中接收这些整数的和,如下所示:

这是主要的过程类:

.meta

这是子流程类:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ParentClass {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            Process p = Runtime.getRuntime().exec("java -cp C:\\Users\\LENOVO\\eclipse-workspace\\TrheadComunication\\bin ChildClass");
            DataOutputStream out = new DataOutputStream(p.getOutputStream());
            DataInputStream in = new DataInputStream(p.getInputStream());
            in.read();
            int a = scanner.nextInt();
            out.writeInt(a);
            out.flush();
            int b = scanner.nextInt();
            out.writeInt(b);
            out.flush();
            int resultat = 20;
            while (resultat == 20)
                resultat = in.read();
            System.out.println(a + " + " + resultat);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

请注意,我添加了循环和睡眠指令,作为使它们无法协同工作的失败尝试。

0 个答案:

没有答案