You could just use Executors.newCachedThreadPool()
, whose doc says:
Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.
This shuts down the threads, not the entire executor, but that should be fine if the executor isn't actually taking any resources.