简体   繁体   English

缺陷固定线程池的缺点

[英]Drawbacks to an idling fixed threadpool

I'm currently in the process of doing various performance improvements in a software. 我目前正在对软件进行各种性能改进。 As it is using SWT for it's GUI, I have come across a problem where under certain circumstances a lot of UI elements are created in the Display Thread. 由于它使用SWT作为它的GUI,我遇到了一个问题,在某些情况下,在显示线程中创建了很多UI元素。 Since the person before me, didn't really take care to do any calculations outside of the Display Thread, the whole software can be unresponsive for several seconds on startup. 由于我之前的人,并没有真正注意在显示线程之外进行任何计算,整个软件在启动时几秒钟内都没有响应。 I've now isolated the code that needs to be performed in the Display Thread, and I'm now calculating everything else in Runnables that I submit to a fixed Threadpool. 我现在已经隔离了需要在Display Thread中执行的代码,现在我正在计算Runnables中我提交给固定Threadpool的所有其他内容。 I'm using the pool like this: 我正在使用这样的游泳池:


public abstract class AbstractChartComposite {
private static ExecutorService pool = Executors.newFixedThreadPool(8);

private List<String> currentlyProcessingChartItems = new ArrayList<>();

protected void doCalculate(constraints){
for (IMERuntimeConstraint c : constraints) {
    if(!currentlyProcessingChartItems.contains(c.getId())){
        currentlyProcessingChartItems.add(c.getId());
        pool.submit(new Runnable(){
            @Override
            public void run() {
                try{
                  createChartItem(c);
                currentlyProcessingChartItems.remove(c.getId());
                }catch(Throwable e){
                    e.printStackTrace();
                }
            }
        });
    }
  }
}
}

I'm now wondering, if I have any drawbacks to leaving the Threadpool running at idle, once all the UI elements have been created. 我现在想知道,一旦创建了所有UI元素,如果让Threadpool在空闲状态下运行有任何缺点。 I cannot really shut it down for garbage collection, because it will be needed again on user Input when a new element needs to be created. 我无法真正关闭垃圾收集,因为当需要创建新元素时,将再次需要用户输入。 So are there any major drawbacks on leaving a threadpool with no submitted Runnables running? 离开线程池没有提交运行的Runnables有什么主要缺点吗?

No, there are no drawbacks. 不,没有缺点。

The threads won't be actually running , they'll be parked until a new task is submitted. 线程将不会实际运行 ,它们将被停放,直到提交新任务。 So it does not affect CPU. 所以它不会影响CPU。 Also you say that you will use this pool again, so in your case there's no point of shutting it down and recreating again. 另外你说你将再次使用这个池,所以在你的情况下,没有必要关闭它并重新创建。

As to the memory - yes, idle threads will consume some amount of memory, but that's not an issue as well, until you have hundreds (thousands?) of threads. 至于内存 - 是的,空闲线程会消耗一些内存,但这也不是问题,直到你有数百(数千?)个线程。

Also, a piece of advice. 另外,一条建议。 Do not do premature optimizations. 不要做过早的优化。 That's the root of all evil. 这是万恶之源。 Analyze a problem once you have real performance issues, using special utilities for that and detecting bottlenecks. 一旦遇到真正的性能问题,使用特殊实用程序并检测瓶颈,就可以分析问题。

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

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