ExecutorService和SwingWorker(ExecutorService and SwingWorker)
我已经看了一遍这个答案,但似乎无法找到它,所以如果这是一个愚蠢的问题,请道歉。 请温柔。
我正在编写一个简单的MVC框架,我对
SwingWorker
以及它如何与ExecutorService
工作时感到困惑。我想通过使用
Executors.newFixedThreadPool(n)
限制ExecutorService允许我的线程数。我理解使用SwingWorker的重要性,因为它具有执行冗长任务(
doInBackground
...等)的方法,并允许通过事件调度线程更改GUI。但是,创建
Executors.newFixedThreadPool(n)
会将线程数限制为n
而SwingWorker
似乎限制为由某种私有内部常量MAX_WORKER_THREADS
设置的10个线程。我想知道的是我咆哮错误的树试图结合这两个类来限制线程的数量,因为暗示似乎是如果我向
Executor
提交SwingWorker
任务,那么一个任务会产生多达十个线程?我最终的目标是编写一个程序,这将有一些密集的计算,我不想从一开始就犯这样一个根本的错误,因为这可能被证明是一个资源消耗问题,也就是说,我会有一个可爱的响应通过正确使用EDT进行GUI,但如果每个任务最多生成10个自己的线程,它将变得无法响应!
I have looked all over for this answer, but can not seem to find it, so apologies if this is a dumb question. Please be gentle.
I am writing a simple MVC framework, and am getting confused on
SwingWorker
and how it works withExecutorService
.I want to limit the number of threads which ExecutorService permits me to by using
Executors.newFixedThreadPool(n)
.I understand the importance of using SwingWorker as it has methods for performing a lengthy task (
doInBackground
...etc) and allows changes to the GUI via the Event Dispatch Thread.However, creating
Executors.newFixedThreadPool(n)
limits the number of threads ton
whereasSwingWorker
seems to have a limit of 10 threads set by some kind of private internal constantMAX_WORKER_THREADS
.What I want to know am I barking up the wrong tree trying to combine these two classes to limit the number of threads, as the implication seems to be if I submit a
SwingWorker
task to anExecutor
, will that one task spawn up to ten threads?My eventual aim is to write a program which will have some intensive computations, and I do not want to make such a fundamental error from the outset, as this could prove to be a resourcing consumption problem, that is, I will have a lovely responsive GUI by proper use of the EDT, but then it will become unresponsive if each task is spawning up to 10 threads of its own!
原文:https://stackoverflow.com/questions/22133484