Questions tagged [threadpool]

A thread pool is a method to reuse existing threads, rather than always creating new ones. They allow for pooling resources in given limits and automatically assigning tasks to open workers. Use this tag when you have questions about implementing a thread pool, or using an existing thread pool implementation.

In multi-threaded applications, handling the various threads is a complicated task. Thread pool functionality is targeted to make it easier to use a large number of threads efficiently.

Because thread creation is relatively expensive, reusing threads in a pool can increase a programs performance. It can also help make better use of the available hardware threads by limiting the total number of threads that are created.

The main goal when using a thread pool is to be able to dynamically schedule work to a given set of workers. However, the original thread does not need to keep track of the thread creation itself, but forwards the thread management to the thread pool implementation. The thread pool will create new workers if necessary and destroy unused ones.

References

Related tags

4676 questions
555
votes
9 answers

If my interface must return Task what is the best way to have a no-operation implementation?

In the code below, due to the interface, the class LazyBar must return a task from its method (and for argument's sake can't be changed). If LazyBars implementation is unusual in that it happens to run quickly and synchronously - what is the best…
Jon Rea
  • 9,337
  • 4
  • 32
  • 35
402
votes
12 answers

How many threads is too many?

I am writing a server, and I send each action of into a separate thread when the request is received. I do this because almost every request makes a database query. I am using a threadpool library to cut down on construction/destruction of…
ryeguy
  • 65,519
  • 58
  • 198
  • 260
292
votes
20 answers

Naming threads and thread-pools of ExecutorService

Let's say I have an application that utilizes the Executor framework as such Executors.newSingleThreadExecutor().submit(new Runnable(){ @Override public void run(){ // do stuff } } When I run this application in the debugger, a…
mre
  • 43,520
  • 33
  • 120
  • 170
241
votes
16 answers

ExecutorService, how to wait for all tasks to finish

What is the simplest way to to wait for all tasks of ExecutorService to finish? My task is primarily computational, so I just want to run a large number of jobs - one on each core. Right now my setup looks like this: ExecutorService es =…
george smiley
  • 2,721
  • 4
  • 21
  • 13
195
votes
12 answers

Thread pooling in C++11

Relevant questions: About C++11: C++11: std::thread pooled? Will async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation? About Boost: C++ boost thread reusing threads boost::thread and creating a pool of…
Yktula
  • 14,179
  • 14
  • 48
  • 71
151
votes
11 answers

Thread vs ThreadPool

What is the difference between using a new thread and using a thread from the thread pool? What performance benefits are there and why should I consider using a thread from the pool rather than one I've explicitly created? I'm thinking specifically…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
150
votes
6 answers

How to get thread id from a thread pool?

I have a fixed thread pool that I submit tasks to (limited to 5 threads). How can I find out which one of those 5 threads executes my task (something like "thread #3 of 5 is doing this task")? ExecutorService taskExecutor =…
serg
  • 109,619
  • 77
  • 317
  • 330
142
votes
1 answer

Does async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation?

It is loosely related to this question: Are std::thread pooled in C++11?. Though the question differs, the intention is the same: Question 1: Does it still make sense to use your own (or 3rd-party library) thread pools to avoid expensive thread…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
127
votes
15 answers

When to use thread pool in C#?

I have been trying to learn multi-threaded programming in C# and I am confused about when it is best to use a thread pool vs. create my own threads. One book recommends using a thread pool for small tasks only (whatever that means), but I can't seem…
creedence.myopenid.com
113
votes
11 answers

Using ThreadPool.QueueUserWorkItem in ASP.NET in a high traffic scenario

I've always been under the impression that using the ThreadPool for (let's say non-critical) short-lived background tasks was considered best practice, even in ASP.NET, but then I came across this article that seems to suggest otherwise - the…
Michael Hart
  • 5,109
  • 3
  • 22
  • 21
112
votes
9 answers

What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

I am learning to use ExectorService to pool threads and send out tasks. I have a simple program below import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Processor…
brain storm
  • 30,124
  • 69
  • 225
  • 393
111
votes
3 answers

FixedThreadPool vs CachedThreadPool: the lesser of two evils

I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of…
110
votes
2 answers

Maximum (client request) thread pool size in spring

I am developing application server using spring boot app but now I want to know what is the default maximum (client request) thread pool size in spring and how can I customize that value?
sagar
  • 1,269
  • 2
  • 10
  • 10
104
votes
2 answers

What's the difference between ThreadPool vs Pool in the multiprocessing module?

Whats the difference between ThreadPool and Pool in multiprocessing module. When I try my code out, this is the main difference I see: from multiprocessing import Pool import os, time print("hi outside of main()") def hello(x): print("inside…
ozn
  • 1,990
  • 3
  • 26
  • 37
96
votes
9 answers

Turning an ExecutorService to daemon in Java

I am using an ExecutoreService in Java 1.6, started simply by ExecutorService pool = Executors.newFixedThreadPool(THREADS). When my main thread is finished (along with all the tasks processed by the thread pool), this pool will prevent my program…
Antiz
  • 1,424
  • 1
  • 13
  • 20
1
2 3
99 100