Java中带有OS进程的信号灯

时间:2019-03-14 07:29:15

标签: java process semaphore

我正在使用Java的ProcessBuilder执行程序,以便将其作为OS程序运行。

该请求被推到队列中,消费者将从那里消费并运行该请求。在使用者内部,我使用了semaphore(8)和执行程序服务将请求作为线程提交。

下面是相应的代码。

ProcessBuilder bldr = new ProcessBuilder().command(commandArray);
bldr.redirectErrorStream(true);
process = bldr.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String ch;
while ((ch = br.readLine()) != null) {
    output = output + ch;
}
br.close();
result = process.waitFor();
....
private static Semaphore semaphore;
private static ExecutorService threadExecutor;
....
semaphore = new Semaphore(8, true);
threadExecutor = Executors.newFixedThreadPool(8);
....
ExecutorWorkerThread thread = new ExecutorWorkerThread(request);
threadExecutor.submit(thread);
semaphore.acquire();

现在问题是与Semaphore(8)无关,进程是一个接一个地执行的,而不是并行执行的。

我期望正确的行为并做正确的事情吗?请纠正我。让我知道是否需要其他详细信息。

0 个答案:

没有答案