Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when no thread is free in the thread pool and we submit a task to the pool?

Will the thread creating method wait for a thread to get free? can I reduce the number of threads generated using thread pooling?

like image 730
Krishna Avatar asked Feb 21 '14 15:02

Krishna


People also ask

Which happens when more tasks are submitted to a thread Executor than available threads?

Tasks are submitted to a thread pool via an internal queue called the Blocking Queue. If there are more tasks than the number of active threads, they are inserted into the blocking queue for waiting until any thread becomes available.

What happens when thread pool is full?

By default, the MaxThreads of the ThreadPool is very high. Usually you'll never get there, your app will crash first. So when all threads are busy the new tasks are queued and slowly, at most 1 per 500 ms, the TP will allocate new threads.

When using a thread pool What happens to a given thread after it finishes its task?

Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.

What happens when thread pool is exhausted?

This depends on the thread-pool. Some pools are fixed size so no more threads will be added. Other thread-pools are "cached" thread pools so it will reuse a free thread or will create a new one if none are available.


2 Answers

If you use a cached thread pool, the pool will create more threads. However this will only be the maximum needed at any one time and might be far less than the number of tasks you submit.

If you use a fixed size thread pool, it will create a fixed number of threads regardless of whether you give it any tasks, or if you give it more tasks than it can do. It will queue any tasks which are waiting.

Will the thread creating method wait for a thread to get free?

While you could create a queue which did this, this is not the default behaviour. A more common solution is to make the caller execute the task if this is required.

can I reduce the number of threads generated using thread pooling?

Thread pooling is likely to produce far less threads than tasks esp if you limit the number of threads.

like image 192
Peter Lawrey Avatar answered Oct 18 '22 10:10

Peter Lawrey


Will the thread creating method wait for a thread to get free?

That contradicts with your title. You'd normally submit a task and the pool would pass that task to a worker thread when one is available. So you'd not create a thread but submit a task. Whether you wait for the task to be executed or just trigger asynchronous execution (which in most cases would be the default) depends on your system and requirements.

Can I reduce the number of threads generated using thread pooling?

Thread pooling is often used to reduce the number of threads created, i.e. instead of having a thread per task you have a defined (maximum) number of worker threads and thus if #tasks > max threads in pool you'll reduce the number of threads needed.

like image 32
Thomas Avatar answered Oct 18 '22 08:10

Thomas