开发者

java progressbar on command line [threads]

开发者 https://www.devze.com 2023-02-14 21:05 出处:网络
I searched the forum before here and found some implementation examples of a progress bar on command line. Now I have an additional question. I am using the following implementation example:

I searched the forum before here and found some implementation examples of a progress bar on command line. Now I have an additional question. I am using the following implementation example:

p开发者_开发百科ublic void print_progress(int percent){

StringBuilder bar = new StringBuilder(category + ": [");

    for(int i = 0; i < 50; i++){
        if( i < (percent/2)){
            bar.append("=");
        }else if( i == (percent/2)){
            bar.append(">");
        }else{
            bar.append(" ");
        }
    }

    bar.append("]   " + percent + "%     ");
    System.out.print("\r" + bar.toString());
}

But I want to use it in three threads, running at the same time, want to show the progress for every thread in a bar like that on the command line. Right now whenever a thread calls the print_progress() method it overwrites the one of the thread before.

Can someone tell if that is possible, and if yes, how?

Thanks very much in advance.


Representation

On a simple command-line, this is not possible if your progressbar takes a whole line. You can go back to the beginning of a line with \r, but you can't go back to a previous line.

You could display 3 progress bars in the same line, though.

Multi-threading issues

It's complicated to have several threads outputting to a single stream in an orderly fashion. It's much easier to have another thread handle the output, and recibe messages from the working threads.

These messages can take the form of items in a thread-safe queue, signals, bytes through a thread-safe stream, or whatever you are confortable with.


To implement 3 progress bars updating on a terminal / console emulator, you will need a Java library that can do screen manipulation on a terminal emulator ... like classic screen editors did / do.

This SO question asks about availability such libraries.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号