简体   繁体   English

Swing Worker线程不并发

[英]Swing Worker Threads Not Concurrent

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. 似乎当我实例化12个Swing Worker线程时,前六个开始完成它的任务,它完成AND然后最后六个开始并完成。 The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time. 我正在寻找的行为是所有12个线程同时开始工作并同时完成。

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. myTask m将以0到100递增一个进度条,增量为25.我发现前六个线程开始递增的异常行为,它们以100结束,然后最后六个线程从0开始递增,然后结束。

Is there a limiting factor on the amount of Swing Worker threads one may have? 是否存在可能具有的Swing Worker线程数量的限制因素?

The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time. 我正在寻找的行为是所有12个线程同时开始工作并同时完成。

A CountDownLatch is designed for this very purpose. CountDownLatch就是为此目的而设计的。 Here's a good example using a single SwingWorker and a number of subsidiary threads controlled by the latch. 这是使用单个SwingWorker和由锁存器控制的许多辅助线程的一个很好的示例

Your question says: 你的问题是:

The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time. 我正在寻找的行为是所有12个线程同时开始工作并同时完成。

But you can't guarantee for all Swing workers threads starting concurrently and ending at same time. 但是你无法保证所有Swing工作线程同时启动并同时结束。

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. SwingWorker类有一个静态ExecutorService字段,MAX_WORKER_THREADS = 10.我不确定你为什么看到6而不是10.但你不能超过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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM