2

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.

Forin
  • 1,549
  • 2
  • 20
  • 44
  • Elaborate on "output remains correct". Where is it currently wrong? – OneCricketeer Oct 13 '20 at 19:18
  • Currently I'm not sure how to change the state in command line, after completablefuture is done. – Forin Oct 13 '20 at 19:22
  • You mean you want to override `` in the command line? Not print each whole line again with the corresponding changed ``? – akuzminykh Oct 13 '20 at 19:29
  • You need your output in a queue like `0,1,2,3,....N`, or `0,2,3,1...N` is OK ? – Victor Gubin Oct 13 '20 at 19:30
  • @akuzminykh yes, I want to replace the in the command line. – Forin Oct 13 '20 at 19:35
  • @VictorGubin yes, it would be nice to persist the order of strings. – Forin Oct 13 '20 at 19:36
  • Ok, seems like you are looking for terminal (curses) library - see. https://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications i.e. [javacurses](https://sourceforge.net/projects/javacurses/) 2. As well as you can output all tasks at once, and then exact asynchronous task will simply replace their status. – Victor Gubin Oct 13 '20 at 19:39
  • Is it possible to achieve my goal without curses? – Forin Oct 13 '20 at 19:41
  • You could somehow clear the terminal and re-print everything with any new information – OneCricketeer Oct 13 '20 at 20:31
  • @OneCricketeer I was thinking about doing so, then I would have to restructure the codebase a bit, but it should work. But I don't know how to clean and print in the same line, without breaking others. – Forin Oct 14 '20 at 07:14
  • It would depend on the terminal, usually, but you'd have to clear the whole "screen"... One way to work around that would be store a list of lines/objects you always want printed – OneCricketeer Oct 14 '20 at 14:54
  • @OneCricketeer is that efficient? – Forin Oct 14 '20 at 15:18
  • Probably not, but it's the only way I can think to ensure ordering of async actions within a terminal window – OneCricketeer Oct 14 '20 at 15:22
  • @Forin your code should be cross-platform, or only a specific OS needed i.e. just Windows, Mac or Linux or all of them together ? – Victor Gubin Oct 14 '20 at 18:14
  • @VictorGubin the best would be to make it work under all platforms, but firstly I'm focused on Linux – Forin Oct 15 '20 at 08:10

0 Answers0