我有一个子流程类,它计算两个整数的和,然后将其放入DataOutputStream中:
public class SubProcess {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(System.in);
DataOutputStream out = new DataOutputStream(System.out);
int a = in.readInt();
out.writeInt(a);
int b = in.readInt();
out.writeInt(b);
int result = a+b;
out.writeInt(result);
out.flush();
in.read();
out.close();
in.close();
}
}
当像12和47分别写a和b的两个值时,结果为“ ei”。
另一方面,主进程不会通过ReadInt()行像DataInputStream那样读取该结果,并且会引发异常:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at testthread.MainProcess.main(MainProcess.java:21)
似乎主流程与子流程实际上没有进行通信。从两个类中删除该程序包并在cmd上运行主进程可避免出现异常。