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.
4,706
questions
561
votes
9
answers
267k
views
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 ...
411
votes
13
answers
334k
views
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 ...
296
votes
21
answers
201k
views
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
}
}...
245
votes
17
answers
274k
views
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 ...
200
votes
12
answers
339k
views
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 ...
154
votes
11
answers
89k
views
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'...
151
votes
6
answers
235k
views
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")?
...
145
votes
1
answer
56k
views
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 ...
127
votes
15
answers
70k
views
When to use thread pool in C#? [closed]
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 ...
116
votes
2
answers
170k
views
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?
116
votes
9
answers
132k
views
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....
114
votes
11
answers
44k
views
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 ...
110
votes
3
answers
91k
views
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 ...
108
votes
2
answers
95k
views
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 ...
98
votes
9
answers
54k
views
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 ...
94
votes
8
answers
55k
views
Java: ExecutorService that blocks on submission after a certain queue size [duplicate]
I am trying to code a solution in which a single thread produces I/O-intensive tasks that can be performed in parallel. Each task have significant in-memory data. So I want to be able limit the number ...
84
votes
2
answers
37k
views
ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew
What is difference between the below
ThreadPool.QueueUserWorkItem
vs
Task.Factory.StartNew
If the above code is called 500 times for some long running task, does it mean all the thread pool threads ...
83
votes
6
answers
26k
views
Should i use ThreadPools or Task Parallel Library for IO-bound operations
In one of my projects that's kinda an aggregator, I parse feeds, podcasts and so from the web.
If I use sequential approach, given that a large number of resources, it takes quite a time to process ...
80
votes
4
answers
35k
views
How to configure a fine tuned thread pool for futures?
How large is Scala's thread pool for futures?
My Scala application makes many millions of future {}s and I wonder if there is anything I can do to optimize them by configuring a thread pool.
Thank ...
71
votes
7
answers
72k
views
C# - ThreadPool vs Tasks
As some may have seen in .NET 4.0, they've added a new namespace System.Threading.Tasks which basically is what is means, a task. I've only been using it for a few days, from using ThreadPool.
Which ...
70
votes
4
answers
85k
views
How to create a thread pool using boost in C++?
How do I create a thread pool using boost in C++, and how do I assign tasks to the threadpool?
64
votes
3
answers
36k
views
Python 3: does Pool keep the original order of data passed to map?
I have written a little script to distribute workload between 4 threads and to test whether the results stay ordered (in respect to the order of the input):
from multiprocessing import Pool
import ...
62
votes
2
answers
90k
views
Code for a simple thread pool in C# [closed]
Looking for some sample code (C#) for a simple thread pool implementation.
I found one on codeproject, but the codebase was just huge and I don't need all that functionality.
This is more for ...
60
votes
2
answers
37k
views
Not much difference between ASP.NET Core sync and async controller actions
I wrote a couple of action methods in a controller to test the difference between sync and async controller actions in ASP.NET core:
[Route("api/syncvasync")]
public class SyncVAsyncController : ...
57
votes
1
answer
35k
views
How are Threads allocated to handle Servlet request?
Can someone please explain what is thread per request and thread per connection? Which model do servlets work on? How threads are allocated to handle HTTP requests? Is it thread/request or connection?
...
56
votes
5
answers
39k
views
What determines the number of threads a Java ForkJoinPool creates?
As far as I had understood ForkJoinPool, that pool creates a fixed number of threads (default: number of cores) and will never create more threads (unless the application indicates a need for those by ...
56
votes
4
answers
20k
views
How Threadpool re-use Threads and how it works
My multithreading concepts are weak and trying to learn.
In Java what I know is, we can't call a thread more than once:
Thread t = new Thread; //Some Runnable
t.start();
t.start(); //Illegal and ...
55
votes
5
answers
116k
views
How can I shutdown Spring task executor/scheduler pools before all other beans in the web app are destroyed?
In a Spring web application I have several DAO and service layer beans. One service layer bean has annotated @Async / @Scheduled methods. These methods depend on other (autowired) beans.
I have ...
53
votes
18
answers
32k
views
Ensuring task execution order in ThreadPool
I have been reading about the thread-pool pattern and I can't seem to find the usual solution for the following problem.
I sometimes want tasks to be executed serially. For example, I read chunks of ...
53
votes
3
answers
14k
views
Why does Windows 10 start extra threads in my program?
With Visual Studio 2015, in a new, empty C++ project, build the following for Console application:
int main() {
return 0;
}
Set a break point on the return and launch the program in the debugger....
53
votes
6
answers
19k
views
Is it really my job to clean up ThreadLocal resources when classes have been exposed to a thread pool?
My use of ThreadLocal
In my Java classes, I sometimes make use of a ThreadLocal mainly as a means of avoiding unnecessary object creation:
@net.jcip.annotations.ThreadSafe
public class ...
52
votes
3
answers
125k
views
multiprocessing.Pool - PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
multiprocessing.Pool is driving me crazy...
I want to upgrade many packages, and for every one of them I have to check whether there is a greater version or not. This is done by the check_one function....
50
votes
5
answers
31k
views
WaitAll for multiple handles on a STA thread is not supported
Why do I get this error message? "WaitAll for multiple handles on a STA thread is not supported."
Should I use [MTAThreadAttribute] attribut? Update: Dosn't work with WPF applications!
Note:
It ...
50
votes
7
answers
36k
views
Thread.Start() versus ThreadPool.QueueUserWorkItem()
The Microsoft .NET Base Class Library provides several ways to create a thread and start it. Basically the invocation is very similar to every other one providing the same kind of service: create an ...
48
votes
7
answers
7k
views
Why are OS threads considered expensive?
There are many solutions geared toward implementing "user-space" threads. Be it golang.org goroutines, python's green threads, C#'s async, erlang's processes etc. The idea is to allow concurrent ...
46
votes
5
answers
45k
views
How to use a goroutine pool
I want to use Go for downloading stock price spreadsheets from Yahoo finance. I'll be making an http request for every stock in its own goroutine. I have a list of around 2500 symbols, but instead of ...
45
votes
6
answers
21k
views
How to catch exceptions from a ThreadPool.QueueUserWorkItem?
I have the following code that throws an exception:
ThreadPool.QueueUserWorkItem(state => action());
When the action throws an exception, my program crashes. What is the best practice for ...
45
votes
6
answers
28k
views
Exception handling in ThreadPools
I have a ScheduledThreadPoolExecutor that seems to be eating Exceptions. I want my executor service to notify me if a submitted Runnable throws an exception.
For example, I'd like the code below to ...
43
votes
7
answers
49k
views
C++ Thread Pool [closed]
What is a good open source implementation of a thread pool for C++ to use in production code (something like boost)?
Please provide either your own example code or a link to example code usage.
42
votes
4
answers
27k
views
how are concurrent requests handled in PHP (using - threads, thread pool or child processes)
I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer
How does server manages multiple connections does it ...
41
votes
1
answer
34k
views
What are the differences between event-driven and thread-based server system?
Node.js is an event driven I/O and It's a single threaded server that
acts upon callbacks and never blocks on the main thread.
But how does it manage to non-blocking I/O?
if it does easy to manage, ...
40
votes
6
answers
41k
views
Propagating ThreadLocal to a new Thread fetched from a ExecutorService
I'm running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here) (the thread "spawning" takes place in a AOP Aspect).
Now, the main thread is a ...
39
votes
7
answers
29k
views
What is the use of a Thread pool in Java?
What is the use of a Thread pool? Is there a good real world example?
38
votes
3
answers
36k
views
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 ...
38
votes
4
answers
36k
views
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 ...
38
votes
4
answers
30k
views
Existing threadpool C implementation [closed]
What open-source implementation(s) in C for a pthreads thread pool would you recommend ?
Additional points if this implementation is :
Light-weight: glib, APR, NSPR and others come with a big buy-in,...
38
votes
3
answers
10k
views
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 ...
38
votes
1
answer
1k
views
Calling condition.wait() inside thread causes retrieval of any future to block on main thread
I have tasks that are executed inside a threadpool that share a reentrant read write lock. these tasks return futures if there execution finishes. The reentrant read write lock will wait on a ...
37
votes
6
answers
50k
views
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 ...
37
votes
5
answers
52k
views
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 ...