Swing Worker线程不并发 [英] Swing Worker Threads Not Concurrent

查看:107
本文介绍了Swing Worker线程不并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎当我实例化12个Swing Worker线程时,前六个开始完成它的任务,它完成AND然后最后六个开始并完成。我正在寻找的行为是所有12个线程同时开始工作&在同一时间结束。

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.

我有以下内容:

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

myTask m会将进度条从0增加到100,增量为25我得到了一个古怪的行为,即前六个线程开始递增,它们以100结束,然后最后六个线程从0开始并递增,并完成。

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.

对于一个人可能拥有的Swing Worker线程数量是否存在限制因素?

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

推荐答案


我正在寻找的行为是所有12个线程同时开始工作&同时完成。

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

A CountDownLatch 就是为此目的而设计的。这是一个很好的示例使用一个 SwingWorker 以及由锁存器控制的一些辅助线程。

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.

这篇关于Swing Worker线程不并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆