简体   繁体   中英

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. 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. 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. 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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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