10
votes

I have a fixed thread pool that runs 7 concurrent threads at any time (with a queue), and I want to turn it into a scheduled thread pool that runs only 7 concurrent jobs but can queue/schedule more.

Reading the documentation didn't really help me..

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads)

Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

Parameters: nThreads - the number of threads in the pool Returns: the newly created thread pool

newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.

Parameters: corePoolSize - the number of threads to keep in the pool, even if they are idle. Returns: a newly created scheduled thread pool

What I don't understand is, are corePoolSize and nThreads the same thing? Is a scheduled thread pool really a subset of a fixed thread pool, meaning that I can use scheduled thread pool as a fixed thread pool that can queue delayed tasks?

3
Could you elaborate on why you want something other than a fixed thread pool? Have you read the javadoc for ScheduledExecutorService?Daniel Lubarov
I am currently using a FixedThreadPool but would like to be able to also schedule tasks after a given delay.. eg. schedule tasks an hour from now, for example. as well as being able to execute immidiately as I am doing today.Mohamed Nuur
It could've been called a Executors.newFixedScheduledThreadPool(n) but thats alot of letters :/John Vint

3 Answers

7
votes

Yes, they are basically the same thing, just with added scheduling functionality. The ScheduledThreadPoolExecutor even extends the default implementation of the ExecutorService (ThreadPoolExecutor).

nThreads and corePoolSize is the number of threads to be spawned. For a fixed executor, it's always the same. With the other implementation, it varies between min (corePoolSize) and max (maxPoolSize).

1
votes

Yes, it works that way in JDK5-6. While in principle the ScheduledExecutorService interface is silent on the issue of pool size, the actual implementation of it used in JDK, uses a fixed pool:

Class ScheduledThreadPoolExecutor

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect.

Obviously that may not hold true if you use a different implementation of ScheduledExecutorService provided by an application framework or a different vendor.

0
votes

Yes they are exactly the same with regard to thread pool size: they ultimately both call the same ThreadPoolExecutor constructor.