Swing Worker Threads Not Concurrent
Asked Answered
E

3

6

It seems that when I instantiate 12 Swing Worker threads, the first six starts to complete its task, it finishes AND then the last six starts and finishes. The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time.

I have the following:

for (int i = 0; i < 12; i++ )
{
        myTask m = new Mytask(i, START);
        m.execute();
}

The myTask m will increment a progress bar from 0 - 100 in increments of 25. I'm getting outlandish behavior that the first six threads start incrementing, they finish at 100, then the last six threads start from 0 and increment, and finish.

Is there a limiting factor on the amount of Swing Worker threads one may have?

Exempt answered 27/8, 2010 at 18:43 Comment(0)
G
7

The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time.

A CountDownLatch is designed for this very purpose. Here's a good example using a single SwingWorker and a number of subsidiary threads controlled by the latch.

Gibby answered 27/8, 2010 at 22:22 Comment(2)
my learn item of the day :-) Only don't like one aspect of the example: the tasks access the textComponent off the EDT (which at that time still was documented to be be thread-safe ... which always was argued ...) In short: better not rely on it and wrap in an invokeLaterNanine
@kleopatra: Well spotted! This example had a similar (latent) problem; its display() method illustrates your suggestion.Gibby
H
2

Your question says:

The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time.

But you can't guarantee for all Swing workers threads starting concurrently and ending at same time.

Harlan answered 27/8, 2010 at 18:45 Comment(3)
Why not? What's the reasoning behind that?Exempt
Because they are Threads, And execution of Threads depends upon multiple facyors, cpu no., OS , available resources etc. etc.Harlan
See my answer for an alernative.Gibby
E
2

SwingWorker class has a static ExecutorService field with MAX_WORKER_THREADS = 10. I'm not certain why you see 6 and not 10. But you cannot go over 10.

    /**
     * number of worker threads.
     */
    private static final int MAX_WORKER_THREADS = 10;

...

    executorService =
                new ThreadPoolExecutor(1, MAX_WORKER_THREADS,
                                       10L, TimeUnit.MINUTES,
                                       new LinkedBlockingQueue<Runnable>(),
                                       threadFactory);
Excurved answered 27/8, 2010 at 18:46 Comment(3)
Can I manually change the # of MAX_WORKER_THREADS or is that not possible? What's the reasoning of having an arbritrary number of 10 threads?Exempt
No, you cannot change the # of MAX_WORKER_THREADS, since it's a final field. As for the reasoning, you need to ask Igor Kushnirskiy (the author of the class).Excurved
You could use your own ExecutorService to execute the SwingWorkers, cause they are FuturesMurder

© 2022 - 2024 — McMap. All rights reserved.