This happens about 75% of time for me under Solaris native threads. It happens with 1.2beta4 and 1.2fcs build E. I have not seen it happen with 1.1 or with green threads or on Win32.
BufferedReader.readLine (or DataInputStream.readline, for that matter) does not return in the example below even though there is something for it to read. If the sleep() call is removed, the immediate termination of the exec'ed process seems to wake up the original process.
Curiously, if I hit Ctrl-\ for a thread dump, the readLine call immediately wakes up and the read line is displayed.
------ Exec.java
import java.util.*;
import java.io.*;
public class Exec {
public static void main(String argv[]) throws Exception {
System.out.println("Exec'ing...");
Process pr = Runtime.getRuntime().exec("java HelloWorld");
BufferedReader in = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
String line = in.readLine();
System.out.println("Line: " + line);
}
}
------ HelloWorld.java
public class HelloWorld {
static public void main(String[] args) throws Exception {
System.out.println("Hello, World!");
/* Same problem occurs if the code below is used
* for (int i = 0; i < 1000; i++) {
* System.out.println("Hello, World!");
* }
* System.out.flush();
*
* Problem goes away if the process is allowed to terminate
* immediately by removing the line below.
*/
Thread.currentThread().sleep(1000000);
}
}
BufferedReader.readLine (or DataInputStream.readline, for that matter) does not return in the example below even though there is something for it to read. If the sleep() call is removed, the immediate termination of the exec'ed process seems to wake up the original process.
Curiously, if I hit Ctrl-\ for a thread dump, the readLine call immediately wakes up and the read line is displayed.
------ Exec.java
import java.util.*;
import java.io.*;
public class Exec {
public static void main(String argv[]) throws Exception {
System.out.println("Exec'ing...");
Process pr = Runtime.getRuntime().exec("java HelloWorld");
BufferedReader in = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
String line = in.readLine();
System.out.println("Line: " + line);
}
}
------ HelloWorld.java
public class HelloWorld {
static public void main(String[] args) throws Exception {
System.out.println("Hello, World!");
/* Same problem occurs if the code below is used
* for (int i = 0; i < 1000; i++) {
* System.out.println("Hello, World!");
* }
* System.out.flush();
*
* Problem goes away if the process is allowed to terminate
* immediately by removing the line below.
*/
Thread.currentThread().sleep(1000000);
}
}