I'm writing a command line application. I have list of strings, which are currently executed synchronously. I would like to make the execution a bit faster, so I was thinking about CompletableFuture
. It solves my problem, but only partially.
In the time of execution I want print the process in the command line, something like this:
> Executing str1... <State>
> Executing str2... <State>
...
> Executing str3... <State>
Where State
is updated after CompletableFuture
is done.
Currently I have:
List<String> strs = someInput();
strs.forEach(str -> execute(str));
and:
private void execute(String str) {
System.out.format("> Executing %s...", str);
Optional<State> stateOptional = heavyTask(str);
stateOptional.ifPresentOrElse(s -> System.out.format("%s%n", s.getState()),
() -> System.out.format("%s%n", State.FAILED.getState()));
}
And State
:
enum State {
DONE, FAILED;
...
}
I'm not sure, how to add the CompletableFuture
so the console output remains correct. I have tried adding the parallelStream()
, but the output is messy.