Skip to content

Thread pool and executor

Category: Units of execution · Status: stub · Lessons: chapter 01, Threads

One line: A set of threads that run submitted tasks one after another, so the cost of starting a thread is paid once rather than once per task.

Also called: executor, ExecutorService, ThreadPoolExecutor.

How it connects

flowchart LR
  n_openmp["OpenMP"]
  n_parallel_iterators["Parallel iterators and streams"]
  n_thread["Thread"]
  n_thread_pool["Thread pool and executor"]
  n_openmp -->|uses| n_thread_pool
  n_parallel_iterators -->|uses| n_thread_pool
  n_thread_pool -->|uses| n_thread
  classDef center stroke-width:3px
  class n_thread_pool center
  classDef outside stroke-dasharray: 4 3
  class n_openmp,n_parallel_iterators,n_thread outside

In each language

Rust None in the standard library; the Rayon crate has ThreadPool
Go No pool type: a fixed number of goroutines receiving from one channel, as in bounded parallelism ↗
Java ExecutorService, from the Executors factories or ThreadPoolExecutor
Python ThreadPoolExecutor and ProcessPoolExecutor
C# The managed thread pool ↗: one per process, used by Task
Kotlin Dispatchers.Default and Dispatchers.IO are shared pools of threads
Swift Dispatch ↗ queues, run on threads the system manages

Where to read more