Skip to content

Commit d40875c

Browse files
committed
Merge pull request #488 from bappelt/update_threadpool_documentation
Update thread pool documentation
2 parents 935c1ea + 9f225c4 commit d40875c

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

doc/thread_pools.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ If you'd like to configure a maximum number of threads, you can use the more gen
4848

4949
A [ThreadPoolExecutor](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ThreadPoolExecutor.html) is a general-purpose thread pool that can be configured to have various behaviors.
5050

51-
The `CachedThreadPool` and `FixedThreadPool` are simply `ThreadPoolExecutor`s with certain configuration pre-determined. For instance, to create a `ThreadPoolExecutor` that works just like a `FixedThreadPool.new 5`, you could:
51+
A `ThreadPoolExecutor` will automatically adjust the pool size according to the bounds set by `min-threads` and `max-threads`.
52+
When a new task is submitted and fewer than `min-threads` threads are running, a new thread is created to handle the request, even if other worker threads are idle.
53+
If there are more than `min-threads` but less than `max-threads` threads running, a new thread will be created only if the queue is full.
54+
55+
The `CachedThreadPool` and `FixedThreadPool` are simply `ThreadPoolExecutors` with certain configuration pre-determined. For instance, to create a `ThreadPoolExecutor` that works just like a `FixedThreadPool.new 5`, you could:
5256

5357
~~~ruby
5458
pool = Concurrent::ThreadPoolExecutor.new(
@@ -58,12 +62,14 @@ pool = Concurrent::ThreadPoolExecutor.new(
5862
)
5963
~~~
6064

61-
If you want to provide a maximum queue size, you may also consider the `fallback_policy` -- what will happen if work is posted to a pool when the queue of waiting work has reached the maximum size? Available policies:
65+
If you wants to provide a maximum queue size, you may also consider the `fallback_policy` which defines what will happen if work is posted to a pool when the queue of waiting work has reached the maximum size and no new threads can be created. Available policies:
6266

6367
* abort: Raise a `Concurrent::RejectedExecutionError` exception and discard the task. (default policy)
6468
* discard: Silently discard the task and return nil as the task result.
6569
* caller_runs: The work will be executed in the thread of the caller, instead of being given to another thread in the pool.
6670

71+
For example:
72+
6773
~~~ruby
6874
pool = Concurrent::ThreadPoolExecutor.new(
6975
min_threads: 5,
@@ -73,24 +79,15 @@ pool = Concurrent::ThreadPoolExecutor.new(
7379
)
7480
~~~
7581

76-
Similarly, you can create something similar to a `CachedThreadPool`, but with a maximum number of threads. With an unbounded queue:
77-
78-
~~~ruby
79-
pool = Concurrent::ThreadPoolExecutor.new(
80-
min_threads: 3, # create 3 threads at startup
81-
max_threads: 10, # create at most 10 threads
82-
max_queue: 0, # unbounded queue of work waiting for an available thread
83-
)
84-
~~~
85-
86-
Or, with a variable number of threads like a CachedThreadPool, but with a bounded queue and a fallback_policy:
82+
You can create something similar to a `CachedThreadPool`, but with a maximum number of threads and a bounded queue.
83+
A new thread will be created for the first 3 tasks submitted, and then, once the queue is full, up to an additional 7 threads (10 total) will be created.
84+
If all 10 threads are busy and 100 tasks are already queued, additional tasks will be rejected.
8785

8886
~~~ruby
8987
pool = Concurrent::ThreadPoolExecutor.new(
90-
min_threads: 3, # create 3 threads at startup
88+
min_threads: 3, # create up to 3 threads before queueing tasks
9189
max_threads: 10, # create at most 10 threads
92-
max_queue: 100, # at most 100 jobs waiting in the queue,
93-
fallback_policy: :abort
90+
max_queue: 100, # at most 100 jobs waiting in the queue
9491
)
9592
~~~
9693

lib/concurrent/executor/fixed_thread_pool.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ module Concurrent
116116
#
117117
# * `idletime`: The number of seconds that a thread may be idle before being reclaimed.
118118
# * `max_queue`: The maximum number of tasks that may be waiting in the work queue at
119-
# any one time. When the queue size reaches `max_queue` subsequent tasks will be
120-
# rejected in accordance with the configured `fallback_policy`.
119+
# any one time. When the queue size reaches `max_queue` and no new threads can be created,
120+
# subsequent tasks will be rejected in accordance with the configured `fallback_policy`.
121121
# * `auto_terminate`: When true (default) an `at_exit` handler will be registered which
122122
# will stop the thread pool when the application exits. See below for more information
123123
# on shutting down thread pools.
@@ -171,8 +171,8 @@ module Concurrent
171171

172172
# @!macro [attach] fixed_thread_pool
173173
#
174-
# A thread pool with a set number of threads. The number of threads in the pool
175-
# is set on construction and remains constant. When all threads are busy new
174+
# A thread pool that reuses a fixed number of threads operating off an unbounded queue.
175+
# At any point, at most `num_threads` will be active processing tasks. When all threads are busy new
176176
# tasks `#post` to the thread pool are enqueued until a thread becomes available.
177177
# Should a thread crash for any reason the thread will immediately be removed
178178
# from the pool and replaced.

lib/concurrent/executor/thread_pool_executor.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ module Concurrent
1818
# @!macro [attach] thread_pool_executor
1919
#
2020
# An abstraction composed of one or more threads and a task queue. Tasks
21-
# (blocks or `proc` objects) are submit to the pool and added to the queue.
21+
# (blocks or `proc` objects) are submitted to the pool and added to the queue.
2222
# The threads in the pool remove the tasks and execute them in the order
23-
# they were received. When there are more tasks queued than there are
24-
# threads to execute them the pool will create new threads, up to the
25-
# configured maximum. Similarly, threads that are idle for too long will
26-
# be garbage collected, down to the configured minimum options. Should a
27-
# thread crash it, too, will be garbage collected.
23+
# they were received.
24+
#
25+
# A `ThreadPoolExecutor` will automatically adjust the pool size according
26+
# to the bounds set by `min-threads` and `max-threads`. When a new task is
27+
# submitted and fewer than `min-threads` threads are running, a new thread
28+
# is created to handle the request, even if other worker threads are idle.
29+
# If there are more than `min-threads` but less than `max-threads` threads
30+
# running, a new thread will be created only if the queue is full.
31+
#
32+
# Threads that are idle for too long will be garbage collected, down to the
33+
# configured minimum options. Should a thread crash it, too, will be garbage collected.
2834
#
2935
# `ThreadPoolExecutor` is based on the Java class of the same name. From
30-
# the official Java documentationa;
36+
# the official Java documentation;
3137
#
3238
# > Thread pools address two different problems: they usually provide
3339
# > improved performance when executing large numbers of asynchronous tasks,
@@ -57,8 +63,8 @@ class ThreadPoolExecutor < ThreadPoolExecutorImplementation
5763
#
5864
# @option opts [Integer] :max_threads (DEFAULT_MAX_POOL_SIZE) the maximum
5965
# number of threads to be created
60-
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) the minimum
61-
# number of threads to be retained
66+
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) When a new task is submitted
67+
# and fewer than `min_threads` are running, a new thread is created
6268
# @option opts [Integer] :idletime (DEFAULT_THREAD_IDLETIMEOUT) the maximum
6369
# number of seconds a thread may be idle before being reclaimed
6470
# @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum

0 commit comments

Comments
 (0)