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
94
votes
8 answers

Java: ExecutorService that blocks on submission after a certain queue size

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 of tasks that are pending at a moment. If I…
Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
84
votes
2 answers

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 will be taken up? Or will TPL (2nd option) be…
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
83
votes
6 answers

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 all of them (because of network issues and similar…
81
votes
4 answers

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 you.
user972946
71
votes
7 answers

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 one is more efficient and less resource…
TheAJ
  • 10,485
  • 11
  • 38
  • 57
67
votes
3 answers

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
  • 15,257
  • 12
  • 59
  • 102
62
votes
2 answers

Code for a simple thread pool in C#

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 educational purposes anyways.
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
61
votes
3 answers

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 numpy as np import time import random rows =…
daniel451
  • 10,626
  • 19
  • 67
  • 125
58
votes
2 answers

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 : Controller { [HttpGet("sync")] public…
Carl Rippon
  • 4,553
  • 8
  • 49
  • 64
57
votes
1 answer

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? And let's say if I want to perform a time…
hellojava
  • 4,904
  • 9
  • 30
  • 38
56
votes
4 answers

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 throw Exception at runtime. As far as I know, it…
Jayesh
  • 6,047
  • 13
  • 49
  • 81
56
votes
5 answers

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 using managedBlock). However, using…
Holger Peine
  • 1,079
  • 1
  • 9
  • 13
54
votes
5 answers

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 configured two thread pools in XML:
tvirtualw
  • 963
  • 2
  • 8
  • 13
53
votes
6 answers

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 DateSensitiveThing { private final Date then; public…
David Bullock
  • 6,112
  • 3
  • 33
  • 43
52
votes
18 answers

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 text from a file and for some reason I need the…
nc3b
  • 15,562
  • 5
  • 51
  • 63