Question

I was reading Oracles documentation on ThreadPoolExecutor and in particular about the queuing in case there are more requests than Threads available. However I did not find out what the default settings are.

To instantiate the Executor the only thing I do is:

executor = Executors.newFixedThreadPool(numberOfThreads);

where executor is from java.util.concurrent.ExecutorService and Executor is the one given by java.util.concurrent.Executors.

From there on the only line where the executor appears again is in:

executor.execute(worker);

So as you see, I never set any property about the queue type or size the executor is using.

What I want to know is which of the strategies described in the documentation I linked is being used.

With the implementation as I've shown: Am I using an unbounded queue? Is it bounded? If so what's the default size?

Was it helpful?

Solution

You just need to read the javadoc:

public static ExecutorService newFixedThreadPool(int nThreads)

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.

(emphasis mine)

If you want maximum flexibility, you can use the ThreadPoolExecutor constructor, which will let you choose every detail you want.

OTHER TIPS

See Executors src

public static ExecutorService  newFixedThreadPool(int nThreads) {
         return new ThreadPoolExecutor (nThreads, nThreads,
                                       0L, TimeUnit.MILLISECONDS,
                                       new LinkedBlockingQueue <Runnable >());
}

as we can see it is unbounded LinkedBlockingQueue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top