3

I'm writing an Java application that runs multiple threads the same time. So, I want each thread to print its status to console: one line per thread, like:

Total threads: %d

Thread-1: Progress - %d%
Thread-2: Progress - %d%
Thread-3: Progress - %d%
Thread-9: Progress - %d%

When the thread ends his job, the line removes. Every new thread adds one more line to status bar.

Each line updates every 5 secs, so, the percentage value increases.

Is there a way to make dynamic status like this?

Max Parker
  • 31
  • 3
  • The first thing that pops to my mind is to have a class/object that takes care of printing this status. Then, all threads register/unregister themselves with that object and inform it about their progress when they see fit. The class/object can then run a loop where it prints the current info every 5 seconds. – domsson Mar 02 '17 at 19:14
  • Typically, the console is line buffered, so once you send in a line, you can't *delete* it, just move it farther up the console. Of course, the exception here is carriage return, but that doesn't work for multiple lines. – Jerfov2 Mar 02 '17 at 19:15
  • To be honest though, it isn't exactly clear to me *what* you're having issues with. Is it a question about multithreading? Or rather about how to clear/reprint to the console? – domsson Mar 02 '17 at 19:15
  • 1
    Possible answers might be [this one](http://stackoverflow.com/questions/3095986/how-to-get-backspace-b-to-work-in-eclipses-console) or [this one](http://stackoverflow.com/questions/7522022/how-to-delete-stuff-printed-to-console-by-system-out-println) – M. Prokhorov Mar 02 '17 at 19:17
  • @domdom With reprinting the console. I've tried this: System.out.print("\r" + "Progress is: " + progress()), but it's not possible to use this for multiple lines. – Max Parker Mar 02 '17 at 19:50
  • @M.Prokhorov It's not exactly what I need, there are solutions for one line while I need to update multiple. – Max Parker Mar 02 '17 at 19:53
  • 1
    see this question http://stackoverflow.com/questions/12197963/java-command-line-interface-having-multiple-progress-bars-on-different-lines-us – Mohammad Alavi Apr 19 '17 at 20:11

0 Answers0