Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:09:24+00:00 2026-05-20T10:09:24+00:00

I try to handle the output of different runnables wihtin another thread. First I

  • 0

I try to handle the output of different runnables wihtin another thread. First I add all runnables to a set and try to trigger their progress, which is saved into a map toether with the category. The category is the identifier for each runnable. There can exist only one runnable per category.

After that I try to write out the output in a progress bar on the stdout. But it is empty (0%) everytime. The strange thing is, when I am debugging in Eclipse, step by step, the progress bar seems to work correctly. I cannot find the problem, maybe it’s some timing problem, or something else.
Can some tell what I am doing wrong?

If someone knows a better way of handling the output of different Threads, please let me know. I am would be happy, definitely.

Thanks in advance for your help.

This is my WriterThread:

public class WriterT extends Thread {

Set<Runnable> my_runnables = new HashSet<Runnable>();
Map<String, Integer> all_runnable_progress = new HashMap<String, Integer>();

public WriterT() {

}


public void add(Runnable r) {
    my_runnables.add(r);
}


public void run() {

    if(!my_runnables.isEmpty()) {

        int progress = 0;

        while(true) {
            for(Runnable r : my_runnables) {
                if(r instanceof Verify_TestRun) {
                    Verify_TestRun run = (Verify_TestRun)r;
                    progress = run.get_progress();
                    all_runnable_progress.put(run.get_category(), progress);
                }
            }

            if(progress <= 100) {
                print_progress();
            } else {
                break;
            }

            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}


private void print_progress() {

    StringBuilder str_builder = new StringBuilder();

    for(String cat : all_runnable_progress.keySet()) {

        int percent = all_runnable_progress.get(cat);

        str_builder.append(cat + "\t[");
        for(int i = 0; i < 25; i++){
            if( i < (percent/4)){
                str_builder.append("=");
            }else{
                str_builder.append(" ");
            }
        }

        str_builder.append("] " + percent + "%" + "\t");
    }

    System.out.print("\r" + str_builder.toString());
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:09:25+00:00Added an answer on May 20, 2026 at 10:09 am

    Updated answer after new information

    So if I understand you correctly, you want to go over each test run you are tracking, see if any of them is still running i.e. the progress is less than 100 and print the progress as long as they’re not all finished.

    First, you need to consider what Stephen C said in his answer – you (probably) want to sum up the progress values of each of the test runs. Then, check if the sum comes out to less than 100 for each test run. If it does, at least 1 test run is still in progress and you print progress and stay in your loop. If you find that your sum comes out to exactly 100 for each test run, then you’re all done. You print progress one final time to update the output to reflect 100% for each and then break from the loop.

    Here is my suggested implementation that makes minor changes to your code:

    public void run() {
        if(!my_runnables.isEmpty()) {
    
            int progress = 0;
    
            while(true) {
                for(Runnable r : my_runnables) {
                    if(r instanceof Verify_TestRun) {
                        Verify_TestRun run = (Verify_TestRun)r;
                        //change #1 - sum up the progress value of each test
                        progress += run.get_progress();
                        all_runnable_progress.put(run.get_category(), progress);
                    }
                }
    
                //change #2 - break when all done
                if(progress < (100 * my_runnables.size()) ) { 
                    //check if tests are still running i.e. there are test runs with progress < 100
                    print_progress();
                } else {
                    //otherwise print one last status (to update all status' to 100%) before stopping the loop
                    print_progress();
                    break;
                }
    
                try {
                    Thread.sleep(150);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    

    Shouldn’t progress be checked within the for loop? What you’re doing right now is iterating over all your Runnables and setting progress to the progress value and adding it to the map. But then you immediately move on to the next Runnable. The net result is that the value of progress once you leave the loop is the value of the last Runnable you handled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a large scale application. I carefully try to handle all
I try to get the main window handle using following code in Lazarus (Free
I try to configure varnish so it can handle jsonp. I use as the
I'm trying to handle the problem where a user can try to open, with
Why doesn't the following doesn't handle the exception that was rethrown? I tried all
I try to write function that return array of string(all files and folders that
I am writing a common lisp program that needs to handle the output of
In a controller a error with Create/Edit ActionResult can be handled with a try-catch
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.