Skip to content

Commit

Permalink
change from synchronized approach to ConcurrentLinkedQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
alfeilex committed Dec 17, 2024
1 parent d7e176e commit ffd6ced
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.cli.CliProcessException;
Expand Down Expand Up @@ -168,7 +169,7 @@ public ProcessResult run(ProcessMode processMode) {

this.processBuilder.command(args);

List<OutputMessage> output = new ArrayList<>();
ConcurrentLinkedQueue<OutputMessage> output = new ConcurrentLinkedQueue<>();

Process process = this.processBuilder.start();

Expand All @@ -188,7 +189,8 @@ public ProcessResult run(ProcessMode processMode) {
exitCode = process.waitFor();
}

ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, output);
List<OutputMessage> finalOutput = new ArrayList<>(output);
ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, finalOutput);

performLogging(result, exitCode, interpreter);

Expand Down Expand Up @@ -219,19 +221,18 @@ public ProcessResult run(ProcessMode processMode) {
* @param is {@link InputStream}.
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<OutputMessage> outputMessages) {
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue<OutputMessage> outputMessages) {

return CompletableFuture.supplyAsync(() -> {

try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {

String line;
while ((line = br.readLine()) != null) {
synchronized (outputMessages) {
OutputMessage outputMessage = new OutputMessage(errorStream, line);
outputMessages.add(outputMessage);
}
OutputMessage outputMessage = new OutputMessage(errorStream, line);
outputMessages.add(outputMessage);
}

return br.lines().toList();
} catch (Throwable e) {
throw new RuntimeException("There was a problem while executing the program", e);
Expand Down

0 comments on commit ffd6ced

Please sign in to comment.