Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Created February 21, 2012 18:32
Show Gist options
  • Select an option

  • Save buzztaiki/1878010 to your computer and use it in GitHub Desktop.

Select an option

Save buzztaiki/1878010 to your computer and use it in GitHub Desktop.
JavaのProcessを使ってるとロックするあれ
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LockedProcess {
private static enum Option {
CHILD( "-child", "Run as child"),
MERGE_ERROR( "-merge-error", "Merge stderr stream into stdout stream"),
CLOSE_STREAM( "-close-stream", "Close all streams before calling waitFor."),
HELP( "-help", "Print this help.");
public final String opt;
public final String desc;
private Option(String opt, String desc) {
this.opt = opt;
this.desc = desc;
}
}
public static void main(String[] args) throws Exception {
System.exit(new LockedProcess().run(args));
}
private int run(String[] args) throws Exception {
CmdLine cl = CmdLine.parse(args);
if (cl.hasHelp()) {
printHelp(System.err);
return 1;
}
if (cl.has(Option.CHILD)) {
return runAsChild(cl);
} else {
return runAsParent(cl);
}
}
private void printHelp(PrintStream out) {
out.format("Usage: java %s [options] <repeat>%n", LockedProcess.class);
for (Option o : Option.values()) {
out.format("\t%-20s%s%n", o.opt, o.desc);
}
}
private int runAsParent(CmdLine cl) throws Exception {
List<String> args = new ArrayList<>();
Collections.addAll(args,
System.getProperty("java.home") + "/bin/java",
LockedProcess.class.getName(),
Option.CHILD.opt);
args.addAll(cl.args);
ProcessBuilder pb = new ProcessBuilder(args);
if (cl.has(Option.MERGE_ERROR)) {
pb.redirectErrorStream(true);
}
Process p = pb.start();
if (cl.has(Option.CLOSE_STREAM)) {
p.getInputStream().close();
p.getErrorStream().close();
p.getOutputStream().close();
}
int n = p.waitFor();
System.out.println("status:" + n);
if (!cl.has(Option.CLOSE_STREAM)) {
drain(p.getInputStream(), System.out);
drain(p.getErrorStream(), System.out);
}
return 0;
}
private int runAsChild(CmdLine cl) {
String slen = or(get(cl.args, 0), "100");
int len = Integer.parseInt(slen);
for (int i = 0; i < len; i++) {
System.out.println("stdout" + i);
System.err.println("stderr" + i);
}
return 0;
}
private void drain(InputStream in, OutputStream out) throws Exception {
for (;;) {
byte[] b = new byte[128];
int n = in.read(b);
if (n <= 0) break;
out.write(b, 0, n);
}
}
private <T> T get(List<T> a, int i) {
return i < a.size() ? a.get(i) : null;
}
private <T> T or(T x, T ifNull) {
return x != null ? x : ifNull;
}
private static class CmdLine {
public final Set<String> opts;
public final List<String> args;
public CmdLine(Set<String> opts, List<String> args) {
this.opts = Collections.unmodifiableSet(new HashSet<>(opts));
this.args = Collections.unmodifiableList(new ArrayList<>(args));
}
public boolean has(Option opt) {
return opts.contains(opt.opt);
}
public boolean hasHelp() {
List<String> helpOpts = Arrays.asList(Option.HELP.opt, "-h", "--help", "-help");
return new HashSet<>(helpOpts).removeAll(opts);
}
public static CmdLine parse(String[] args) {
Set<String> o = new HashSet<>();
List<String> a = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
o.add(args[i]);
} else {
a.add(args[i]);
}
}
return new CmdLine(o, a);
}
}
}
@buzztaiki
Copy link
Copy Markdown
Author

java LockedProcess 5000
java LockedProcess -help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment