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 8146679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:05:14+00:00 2026-06-06T14:05:14+00:00

Unfortunately I have a pretty bad understanding of how to properly set up threading.

  • 0

Unfortunately I have a pretty bad understanding of how to properly set up threading. I know there is a bunch of info on this both here on SO.SE and on other sites but I can’t seem to relate what I read correctly to what I’m doing.

My problem is that I have a method that takes two parameters where one is divided by the other. The quotient (result) is used to fill up a visual progress bar. When the quotient gets to 1, (readBytes/contentLength == 1), I want some thread (I guess) to wait for a given time before the progress bar is removed from the layout. I know all the code needed to set the value to the progress bar and how to remove it from the view, my question is how do I make it wait for, say, 2000 ms before the action is triggered to remove the component?

This is probably basic threading knowledge but I’m having huge problems with it.

So far I’ve tried these two approaches:

@Override
    public void updateProgress(long readBytes, long contentLength) {

        this.contentLength = contentLength;

        if(readBytes != 0 && contentLength != 0 && fileListItem != null)    {

            fileListItem.getProgressIndicator().setValue(readBytes/contentLength);

            synchronized (this) {
                while(readBytes/contentLength != 1) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    };
                    fileListItem.removeProgressIndicator();                 
                }
            }
        }

        if(!itemIsAdded)    {

            checkFileCompatibility(contentLength);

        }
    }

AND

@Override
    public void updateProgress(long readBytes, long contentLength) {

        this.contentLength = contentLength;

        if(readBytes != 0 && contentLength != 0 && fileListItem != null)    {

            if(readBytes/contentLength == 1)    {
                Thread t = new Thread();
                t.start();
                try {
                    t.wait(2000);
                    fileListItem.removeProgressIndicator();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                t.interrupt();
            } else  {
                fileListItem.getProgressIndicator().setValue(readBytes/contentLength);
            }
        }

        if(!itemIsAdded)    {

            checkFileCompatibility(contentLength);

        }
    }

With no success. In the first example the main thread seems to be the one waiting and nothing happens. And in the second example I get an exception on the t.wait(2000);. I’m at a loss of how I should do..

EDIT: With the input from Bohemian I got it working.

@Override
    public void updateProgress(final long readBytes, final long contentLength) {

        this.contentLength = contentLength;

        if(readBytes != 0 && contentLength != 0 && fileListItem != null)    {

            if(!threadIsRunning)    {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        threadIsRunning = true;
                        while(!fileIsAdded) {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                LOGGER.error(e.getMessage());
                                break;
                            }
                        }
                        fileListItem.removeProgressIndicator();
                        threadIsRunning = false;
                    }
                }).start();
            }

            fileListItem.getProgressIndicator().setValue(readBytes/contentLength);
            if(readBytes == contentLength)
                fileIsAdded = true;
        }

        if(!itemIsAdded)    {

            checkFileCompatibility(contentLength);

        }
    }

It still needs some tidying up but the basics are now working!

  • 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-06-06T14:05:16+00:00Added an answer on June 6, 2026 at 2:05 pm

    I wouldn’t have the main thread wait. It’s bad practice because it isn’t scalable and makes your GUI jittery.

    Instead, I would pass a timeout value and a couple of callbacks to the worker thread to execute when it exceeds its timeout/completes its work. That way the main thread is free to go back to doing whatever it wants to.

    Just for illustration purposes, your “completion” callback might look like:

    new Runnable() {
        public void run() {
            // code that hides the progress bar
        }
    }
    

    Your “timeout” callback might look like:

    new Runnable() {
        public void run() {
            // code that displays an error message
        }
    }
    

    By the way, to get a thread to do something, you also pass it a Runnable:

    new Thread(new Runnable() {
        public void run() {
            // code that runs when your thread starts
        }
    }).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that this has been asked many times. But, unfortunately I have not
I have pretty much tried everything but still have this same problem. I have
Hi I love these pretty checkboxes: and have implemented them in my site. Unfortunately
I have been using this website pretty often in order to solve small issues
Unfortunately this is going to be a pretty open-ended question, but I am at
I have a site with a bunch of (pretty ugly) classic asp pages, as
Unfortunately I have to do some interaction with IBM's UniData database system. I am
We have TRAC 0.11 server. Unfortunately we have deleted a milestone in one of
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I have six tables, which unfortunately does not have any primary/foreign key-relations encoded. I've

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.