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
38
votes
3 answers

ThreadPool SetMaxThreads and SetMinThreads Magic Number

Is there a magic number or formula for setting the values of SetMaxThreads and SetMinThreads for ThreadPool? I have thousands of long-running methods that need execution but just can't find the perfect match for setting these values. Any advise…
Benny
  • 3,899
  • 8
  • 46
  • 81
37
votes
4 answers

Java Thread Pool with a Bounded Queue

I'm using java.util.concurrent's Executors class to create a fixed thread pool for running request handlers for a web server: static ExecutorService newFixedThreadPool(int nThreads) and the description is: Creates a thread pool that reuses a…
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
37
votes
11 answers

Multithreaded job queue manager

I need to manage CPU-heavy multitaskable jobs in an interactive application. Just as background, my specific application is an engineering design interface. As a user tweaks different parameters and options to a model, multiple simulations are run…
Marc Ditto
  • 371
  • 1
  • 4
  • 3
36
votes
4 answers

How to name the threads of a thread pool in Java

I have a Java application that uses the Executor framework and I have code that looks like this protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5) My understanding is that internally the JVM would create…
Sudarshan
  • 8,574
  • 11
  • 52
  • 74
36
votes
2 answers

C# thread pool limiting threads

Alright...I've given the site a fair search and have read over many posts about this topic. I found this question: Code for a simple thread pool in C# especially helpful. However, as it always seems, what I need varies slightly. I have looked over…
jason baisden
  • 425
  • 1
  • 6
  • 8
36
votes
3 answers

Why does ScheduledThreadPoolExecutor only accept a fixed number of threads?

I may imagine some tasks scheduled to take a very long time and ScheduledThreadPoolExecutor would create additional threads for the other tasks that need to be run, until a maximum number of threads is reached. But seems that I can only specify a…
Matthew
  • 10,988
  • 11
  • 54
  • 69
36
votes
5 answers

Controlling scheduling priority of python threads?

I've written a script that uses two thread pools of ten threads each to pull in data from an API. The thread pool implements this code on ActiveState. Each thread pool is monitoring a Redis database via PubSub for new entries. When a new entry is…
backus
  • 4,076
  • 5
  • 28
  • 30
35
votes
4 answers

What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool?

Java 5 has introduced support for asynchronous task execution by a thread pool in the form of the Executor framework, whose heart is the thread pool implemented by java.util.concurrent.ThreadPoolExecutor. Java 7 has added an alternative thread pool…
35
votes
1 answer

What is the difference between SynchronizationContext.Send and SynchronizationContext.Post?

Thanks to Jeremy Miller's good work in Functional Programming For Everyday .NET Development, I have a working command executor that does everything I want it to (do heavy lifting on the thread pool, send results or errors back to the synchronization…
flipdoubt
  • 13,897
  • 15
  • 64
  • 96
35
votes
6 answers

How to wait for a ThreadPoolExecutor to finish

My Question: How to execute a bunch of threaded objects on a ThreadPoolExecutor and wait for them all to finish before moving on? I'm new to ThreadPoolExecutor. So this code is a test to learn how it works. Right now I don't even fill the…
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
35
votes
1 answer

Difference between delegate.BeginInvoke and using ThreadPool threads in C#

In C# is there any difference between using a delegate to do some work asynchronously (calling BeginInvoke()) and using a ThreadPool thread as shown below public void asynchronousWork(object num) { //asynchronous work to be done …
nighthawk457
  • 1,102
  • 3
  • 12
  • 27
33
votes
5 answers

AsyncTask.executeOnExecutor() before API Level 11

The normal way we do AsyncTask in Android is, from Android API: private class DoIntenseTask extends AsyncTask { protected Void doInBackground(Object... params) { for (Object param : params) { Object rtnObj =…
yorkw
  • 40,926
  • 10
  • 117
  • 130
33
votes
3 answers

ThreadPool max threads

I've got some trouble with .NET's ThreadPool (.NET 4). I've read that by default .NET has a limit of 25 threads per processor, but according to forum posts on SO and on other places, I can increase the limit with the below code. void SetThreads(int…
foxy
  • 7,599
  • 2
  • 30
  • 34
32
votes
5 answers

How do I manage ruby threads so they finish all their work?

I have a computation that can be divided into independent units and the way I'm dealing with it now is by creating a fixed number of threads and then handing off chunks of work to be done in each thread. So in pseudo code here's what it looks like #…
David K.
  • 6,153
  • 10
  • 47
  • 78
31
votes
10 answers

Removing all queued tasks of an ThreadPoolExecutor

i have this rather simple question about the ThreadPoolExecutor. I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But…
Malax
  • 9,436
  • 9
  • 48
  • 64