import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class SubProcess {
public static final long sleepTime = 1000; // one second
private ThreadWorker threadWorker;
public SubProcess() {
this.threadWorker = new ThreadWorker();
}
public static void main(String[] args) {
PrintStream stream = null;
try {
stream = new PrintStream(new BufferedOutputStream(
new FileOutputStream("subprocess.log", true)));
/*
* see
* http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#setErr(java.io.PrintStream)
* and
* http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#setOut(java.io.PrintStream)
*/
System.setOut(stream);
System.setErr(stream);
new SubProcess().startDigging();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (stream != null)
stream.close();
}
}
private void startDigging() {
this.threadWorker.run();
}
class ThreadWorker implements Runnable {
private int dig(int depth) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
throw new RuntimeException("Caught " + e.getMessage()
+ " at depth " + depth, e);
}
System.out.println("Level #" + depth++);
return dig(depth);
}
public void run() {
dig(0);
}
}
} |