rohitmohta
rohitmohta

Reputation: 1061

Java ThreadPoolExecutor and BlockingQueue to be used

In the Oracle site (http://docs.oracle.com/javase/7/docs/api/?java/util/concurrent/ThreadPoolExecutor.html) under Queuing section, its mentioned that "If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread."

Lot of our code have corePoolSize as 1 and maximumPoolSize are like 10 or 20. (1) Is it a bad practice? or rather not an optimal way to use ThreadPool's? (2) Would you recommend have corePoolSize and maximumPoolSize value be same when creating a ThreadPoolExecutor?

Another question is with respect to usage of a BlockingQueue while creating a ThreadPoolExecutor - between LinkedBlockingQueue or ConcurrentLinkedQueue? I was looking more from the locking and performance point-of-view? Though running a test proves, inserting in a concurrentLinkedQueue is quite fast compared to other. Any thoughts?

Edited:

The first part has been answered in various questions. The one I liked How to get the ThreadPoolExecutor to increase threads to max before queueing?

Upvotes: 6

Views: 2373

Answers (2)

Siva Kumar
Siva Kumar

Reputation: 2006

Please find the bellow points .

  1. If a new task is submitted to the list of tasks to be executed, and less than corePoolSize threads are running, a new thread is created to handle the request. Incoming tasks are queued in case corePoolSize or more threads are running.

  2. If a request can’t be queued or there are less than corePoolSize threads running, a new thread is created unless this would exceed maximumPoolSize.

  3. If the pool currently has more than corePoolSize threads, excess threads will be terminated if they have been idle for more than the keepAliveTime.

  4. If a new task is submitted to the list of tasks to be executed and there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. This means that when using an Unbounded queue, no more threads than corePoolSize will be running and keepAliveTime has no influence.

  5. If corePoolSize and maximumPoolSize are the same, a fixed-size thread pool is used

    Your ThreadPool Size

    Core Pool size is 1.

    Max Pool size is 10 or 20.

Question 1 : Is it a bad practice? or rather not an optimal way to use ThreadPool's?

It is based on your Pool which is used.

If you are using bounded queue , new thread will be created upto max size once it reaches the max limit of bounded queue. Otherwise it will run in core pool size . If you are using unbounded queue it will be Core pool size only.

Question 2 : Would you recommend have corePoolSize and maximumPoolSize value be same when creating a ThreadPoolExecutor?

Yes. So that only you can get same no of threads instead of what kind blocking queue . Even non blocking queue also you can get max no of threads.

Question 3 concurrentLinkedQueue vs LinkedBlockingQueue

You should not allow to use concurrentLinkedQueue since threadpool only supports BlockingQueue. So you may try concurrentLinkedBlockingQueue. Because concurrentLinkedBlockingQueue is unbounded. But LinkedBlockingQueue is bounded.

Upvotes: 4

Oliver
Oliver

Reputation: 6260

Well that should not happen, the reason TPE puts the request(Runnable) in queue is that it should not consume too much memory. But it guarantees the execution of all Runnables. The purpose of thread pool executor is to optimize Memory.

If you want to increase pool size to max before adding in queue then I think you would have to extend the Thread Pool Executor.

Upvotes: 0

Related Questions