Tord Larsen
Tord Larsen

Reputation: 2836

in java how to create a common progress indicator for multiple threads

If I have 5 threads that do work and when they start work they send a progress-start command to lets say a progress wheel that start spinning. When a thread stop work it send a progress-stop to the wheel.

On can easy see that this will not work since the last thread that are still working has gotten his progress-start removed by the last thread who stopped.

I was thinking about some kind of stack fifo/lifo implementation to remember what thread is doing what, and then correctly tell the progress wheel what state it should be in.

But that feels like it can get broken, remembering thread states..

Maybe the progress wheel could run a timer periodically every 3 sec check if threads are alive Any ide?

Upvotes: 0

Views: 973

Answers (2)

Hamzaan Bridle
Hamzaan Bridle

Reputation: 136

I would recommend avoiding stacks or any other order dependent operation when working with threads, there is little to no guarantee on the order of thread completion.

Instead, try using the Future interface to keep track of thread completion. For example when each thread signals to the progress wheel that it has finished the progress wheel can call Future.isDone() on any other tasks that have been previously called to determine if it should stop.

Another advantage of using the Future interface is that you can catch any exceptions or errors (e.g. InterruptedException or ExecutionException) thrown by the task and then send an appropraite message to your progress wheel.

Upvotes: 1

Alexander Daum
Alexander Daum

Reputation: 741

One thing you could do is having a CountDownLatch. The good thing about a CountDownLatch is, that you can make the ProgressWheel as a new thread, and the main thread will just wait until the counter reaches 0. Every WorkerThread would just get passed the CountDownLatch in its constructor and count it down when the run method is finished.

If the worker threads are not started all at once, you would have to use a counter, count it down when a thread finishes, count it up when one start and poll its value in the progress wheel.

Here is a sample code

public static void main(String[] args) {
    ProgressWheel wheel = new ProgressWheel();
    CountDownLatch latch = new CountDownLatch(3);
    wheel.start();
    new WorkerThread(latch).start(); 
    new WorkerThread(latch).start();
    new WorkerThread(latch).start();
    try {
        latch.await(); //wait until counter reaches 0
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    wheel.stop();

}

private static class WorkerThread extends Thread{
    private CountDownLatch counter;
    public WorkerThread(CountDownLatch counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        //Do some heavy work
        counter.countDown(); //This thread is finished
    }
}

Upvotes: 2

Related Questions