I have a Java program that prints to stdout. If the output is transferred to, say, the headshell does not return immediately after the head has completed its task, but expects the Java program to complete all its work.
So my question is: how can I write a Java program so that the shell returns immediately, how cat ... | head?
This is an example of what I mean:
Here, the shell returns immediately because it headdoes not take time to print the first ten lines, no matter how large bigfile.txt:
time cat bigfile.txt | head
...
real 0m0.079s
user 0m0.001s
sys 0m0.006s
Here, instead, the first ten lines are printed quickly, but the shell does not return until the entire file has been processed:
time java -jar DummyReader.jar bigfile.txt | head
...
real 0m18.720s
user 0m16.936s
sys 0m2.212s
DummyReader.jar as simple as I could do this:
import java.io.*;
public class DummyReader {
public static void main(String[] args) throws IOException {
BufferedReader br= new BufferedReader(new FileReader(new File(args[0])));
String line;
while((line= br.readLine()) != null){
System.out.println(line);
}
br.close();
System.exit(0);
}
}
My settings:
java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-10M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
On macOS 10.6.8