AndroidHustle
AndroidHustle

Reputation: 1814

Setting up the correct multi threading

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!

Upvotes: 0

Views: 93

Answers (3)

Marko Topolnik
Marko Topolnik

Reputation: 200148

In all probability SwingWorker is the right tool for your task. There is a full code sample in the Javadoc. Notice the method setProgress -- that's ideal to update your progress bar.

If all you need is really just a fixed 2 second delay for clearing the progress bar, then you want to use a Swing Timer. It doesn't even involve multithreading, you just write a callback handler that will be executed after the specified delay.

Upvotes: 2

Bohemian
Bohemian

Reputation: 424983

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();

Upvotes: 1

Tudor
Tudor

Reputation: 62439

I think what you really need is

Thread.sleep(2000);

instead of wait. While wait can be used for sleeping, its primary function is for inter-thread signaling and requires a lock on the object being waited on, which you have not acquired, hence the exception.

Edit: Upon further inspection I notice that you are not doing things correctly. You are just creating a thread with no associated run method:

Thread t = new Thread();
t.start();

Hence your thread is not executing anything.

I suggest reading a Java threading tutorial on how to set up threads correctly: http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Upvotes: 1

Related Questions