Skip to content

Instantly share code, notes, and snippets.

@froop
Last active August 29, 2015 14:01
Show Gist options
  • Save froop/52e9a4da4fde1c3a6e9e to your computer and use it in GitHub Desktop.
Save froop/52e9a4da4fde1c3a6e9e to your computer and use it in GitHub Desktop.
[Java] Apache Commons Exec のスレッドセーフ化
/**
* Apache Commons Exec の同時起動できない問題への対策.
* 同時に複数が実行開始されると、それら全ての実行完了が一番遅い実行を待ってしまう現象.
* 原因は、Runtime#exec() の呼び出しがマルチスレッドで競合しているため.
* Runtime の呼び出しを synchronized でロックすることで解消.
*/
class ThreadSafeExecutor extends org.apache.commons.exec.DefaultExecutor {
@Override
protected Process launch(CommandLine command, Map env, File dir)
throws IOException {
synchronized (ThreadSafeExecutor.class) {
return super.launch(command, env, dir);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment