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.

Filter by
Sorted by
Tagged with
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 ...
Jon Rea's user avatar
  • 9,397
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 ...
ryeguy's user avatar
  • 66.2k
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 } }...
mre's user avatar
  • 43.9k
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 ...
george smiley's user avatar
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 ...
Yktula's user avatar
  • 14.4k
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'...
Mark Ingram's user avatar
  • 72.8k
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")? ...
serg's user avatar
  • 110k
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 ...
Philipp Claßen's user avatar
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 ...
user avatar
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?
sagar's user avatar
  • 1,329
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....
brain storm's user avatar
  • 30.8k
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 ...
Michael Hart's user avatar
  • 5,159
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 ...
Daniel's user avatar
  • 2,425
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 ...
ozn's user avatar
  • 2,088
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 ...
Antiz's user avatar
  • 1,454
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 ...
Tahir Akhtar's user avatar
  • 11.5k
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 ...
stackoverflowuser's user avatar
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 ...
HuseyinUslu's user avatar
  • 4,114
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 ...
user avatar
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 ...
TheAJ's user avatar
  • 10.7k
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?
Jeroen's user avatar
  • 16.1k
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 ...
daniel451's user avatar
  • 10.8k
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 ...
Blankman's user avatar
  • 264k
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 : ...
Carl Rippon's user avatar
  • 4,603
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? ...
hellojava's user avatar
  • 4,984
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 ...
Holger Peine's user avatar
  • 1,179
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 ...
Jayesh's user avatar
  • 6,077
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 ...
tvirtualw's user avatar
  • 973
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 ...
nc3b's user avatar
  • 15.9k
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....
Adrian McCarthy's user avatar
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 ...
David Bullock's user avatar
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....
rubik's user avatar
  • 8,914
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 ...
Amir Rezaei's user avatar
  • 5,048
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 ...
Andry's user avatar
  • 16.5k
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 ...
Chi-Lan's user avatar
  • 3,655
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 ...
tldr's user avatar
  • 12k
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 ...
Michael Hedgpeth's user avatar
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 ...
Ivan's user avatar
  • 1,256
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.
Amir Rachum's user avatar
  • 78.1k
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 ...
prasun's user avatar
  • 7,223
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, ...
erginduran's user avatar
  • 1,698
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 ...
Luciano Fiandesio's user avatar
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?
JavaUser's user avatar
  • 26k
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 ...
Benny's user avatar
  • 3,899
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 ...
Amir Rachum's user avatar
  • 78.1k
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,...
Mathias Brossard's user avatar
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 ...
Matthew's user avatar
  • 11.1k
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 ...
user avatar
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 ...
kentcdodds's user avatar
  • 28.1k
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 ...
backus's user avatar
  • 4,186

1
2 3 4 5
95