New methods in jdk7
Paulo Levi
i30817 at gmail.com
Tue Oct 6 16:40:14 UTC 2009
I thought there was the danger of deadlocks if the std.out and std.err
wasn't drained?
In fact my implementation of "fork" is like this, mostly because i
read that otherwise deadlocks happened.:
/**
* This method creates a new process that will run a new jvm
* on the main of the given class, with the selected arguments.
* It already flushes the output and inputstream of the forked jvm
* into the current jvm.
* The forked jvm uses the same java.exe and classpath as the current
* one.
* @param javaClass class with main method
* @param args jvm properties.
* @return Process, the jvm process, already started
*/
public static Process forkJava(Class klass, String... args) throws
IOException, InterruptedException {
String javaExe = System.getProperty("java.home") +
File.separator + "bin" + File.separator + "java";
String classpath = System.getProperty("java.class.path");
List<String> l = new ArrayList<String>(4 + args.length);
l.add(javaExe);
l.add("-cp");
l.add(classpath);
l.addAll(Arrays.asList(args));
l.add(klass.getCanonicalName());
ProcessBuilder pb = new ProcessBuilder(l);
pb.redirectErrorStream(true);
final Process p = pb.start();
new Thread(new ProcessStreamConsumer(p),
"ProcessBuilderInputStreamConsumer").start();
return p;
}
To be clear, you're saying that if i call this, and allow the parent
to be killed (by throwing a Error for instance), the child process
lives still? (I believe i can't call System.exit(1) without all jvm
dying right?)
More information about the core-libs-dev
mailing list