ExecutorService itself is another object, so there is no big overhead. But each thread pool has unused threads by default, and this is the reason for the large resource. I would suggest setting the default number of pre-generated threads for each pool (1 or 0 if you are not sure if any requests have been sent) in order to reduce the cost of creating additional objects. Themes will be created on demand and you can keep your code clean.
Another solution is to use a single thread pool, but to maintain a separate task list for each user interface window. In this case, when the window closes, you will have to iterate through all the tasks and manually cancel the launch (this can also be done in a separate thread). The task can be represented by Future Future<?> (It has convenient methods isDone() and cancel() ).
source share