2

From javadoc for ThreadPoolExecutor:

When a new task is submitted in method execute(java.lang.Runnable) .... If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

How can I make ThreadPool that will start new thread instead of submitting task to queue in this situation?

Example:

I have thread with corePoolSize=5 and maxPoolSize=10 and unbounded queue.

Suppose corePoolSize threads is busy right now, and new task is arrived - pool must start new thread and execute new task.

Suppose maxPoolSize threads is busy right now, and new task is arrived - pool must persist task into queue, and first free thread must take this task and execute.

ice
  • 777
  • 1
  • 4
  • 19
  • 4
    Doing that misses the point of using a thread pool in the first place. A thread pool is for reusing a thread for multiple tasks. If you want to start a new thread for each task, just create a new thread yourself, there is no need to use a thread pool. – Abhinav Sarkar Sep 26 '12 at 12:26
  • 2
    The whole point of a thread pool is to reuse existing threads when they are idle. Can you elaborate a little and explain what you want to achieve? – assylias Sep 26 '12 at 12:27
  • I have some restriction - max 100 working threads - this is my maxPoolSize. I need some count of threads to be ready everytime - this is my corePoolSize. And I have unbounded queue. I want to use all 100 threads if I have tasks for each one. – ice Sep 26 '12 at 12:37
  • 1
    Possible duplicate of http://stackoverflow.com/questions/19528304/how-to-get-the-threadpoolexecutor-to-increase-threads-to-max-before-queueing/19528305#19528305 – Gray Oct 22 '13 at 21:37

3 Answers3

2

If you want a thread pool with a maximum size where the tasks are queued only when all the threads are busy.

ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);

The cached thread pool which will not queue tasks, but will create a new thread as required. It will re-use a thread if one is free.

ExecutorService service = Executors.newCachedThreadPool();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yes, something like cached thread pool, but with max size of working threads. And if this max size is reached tasks must be persisted in queue. – ice Sep 26 '12 at 12:40
  • So you do want a queue after all. :) – Peter Lawrey Sep 26 '12 at 12:42
  • In fixedThreadPool all thread either live forever or all will be destroyed after some time. I need some count(but not all) to be live forever. – ice Sep 26 '12 at 12:50
  • You can create a thread pool which expires idle threads, but usually this isn't needed as only the peak usage matters. – Peter Lawrey Sep 26 '12 at 13:01
2

I've just posted this exact question and provided an answer there:

How to get the ThreadPoolExecutor to increase threads to max before queueing?

Sorry to not have found this question beforehand otherwise I would have just answered it.

To summarize the answer, I extend LinkedBlockingQueue to have it always return false for queue.offer(...) which will add an additional threads to the pool, if necessary. If the pool is already at max threads and they all are busy, the RejectedExecutionHandler will be called. It is the handler which then does the put(...) into the queue.

There is sample code in my answer. Again, sorry for the dup.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
1

If you want to start new Thread each time regardless of availability of idle threads, then you need not ThreadPool at all - just create and start new Thread manually. This is better because your threads would die immediately after they do their work, and in the ThreadPool they would wait some time for new job but never get it, thus only wasting memory.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38