I’m trying to use thread pool in Java. But the number of threads is unknown, so I’m trying to find a solution. Then two questions occured:
-
I’m looking for increasing size of thread pool for some time, but I couldn’t come up with something yet. Any suggestions for that? Some say
Executors.newCachedThreadPool()should work but in definition of the method it says it’s for short-time threads. -
What if I set the size of the thread pool as a big number like 50 or 100? Does it work fine?
You can use Executors.newCachedThreadPool for more long-lived tasks also, but the thing is that if you have long running tasks, and they’re added constantly and more frequently than existing tasks are being completed, the amount of threads will spin out of control. In such case it might be a better idea to use a (larger) fixed-size thread pool and let the further tasks wait in queue for a free thread.
This will only mean you’ll (probably) have lots of alive threads that are sleeping (idle) most of the time. Basically the things to consider are
-Xss-parameter)That being said, it’d probably be best to use the ThreadPoolExecutor‘s constructors directly to get the kind of pooling you want.