You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/thread_pools.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
# Thread Pools
2
2
3
-
A Thread Pool is an abstraction that you can give a unit of work to, and the work will be executed by one of possibly several threads in the pool. One motivation for using thread pools is the overhead of creating and destroying threads. Creating a pool of reusable worker threads then repeatedly re-using threads from the pool can have huge performace benefits for a long-running application like a service.
3
+
A Thread Pool is an abstraction that you can give a unit of work to, and the work will be executed by one of possibly several threads in the pool. One motivation for using thread pools is the overhead of creating and destroying threads. Creating a pool of reusable worker threads then repeatedly re-using threads from the pool can have huge performance benefits for a long-running application like a service.
4
4
5
5
`concurrent-ruby` also offers some higher level abstractions than thread pools. For many problems, you will be better served by using one of these -- if you are thinking of using a thread pool, we especially recommend you look at and understand [Future](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Future.html)s before deciding to use thread pools directly instead. Futures are implemented using thread pools, but offer a higher level abstraction.
6
6
7
7
But there are some problems for which directly using a thread pool is an appropriate solution. Or, you may wish to make your own thread pool to run Futures on, to be separate or have different characteristics than the global thread pool that Futures run on by default.
8
8
9
-
Thread pools are considered 'executors' -- an object you can give a unit of work to, to have it exeucted. In fact, thread pools are the main kind of executor you will see, others are mainly for testing or odd edge cases. In some documentation or source code you'll see reference to an 'executor' -- this is commonly a thread pool, or else something similar that executes units of work (usually supplied as ruby blocks).
9
+
Thread pools are considered 'executors' -- an object you can give a unit of work to, to have it executed. In fact, thread pools are the main kind of executor you will see - others are mainly for testing or odd edge cases. In some documentation or source code you'll see reference to an 'executor' -- this is commonly a thread pool, or else something similar that executes units of work (usually supplied as Ruby blocks).
10
10
11
11
## FixedThreadPool
12
12
13
-
A [FixedThreadPool](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/FixedThreadPool.html) contains a fixed number of threads. When you give a unit if work to it, an available thread will be used to execute.
13
+
A [FixedThreadPool](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/FixedThreadPool.html) contains a fixed number of threads. When you give a unit of work to it, an available thread will be used to execute.
14
14
15
15
~~~ruby
16
16
pool =Concurrent::FixedThreadPool.new(5) # 5 threads
@@ -21,15 +21,15 @@ end
21
21
# while work is concurrently being done in the thread pool, at some possibly future point.
22
22
~~~
23
23
24
-
What happens if you post new work when all (eg) 5 threads are currently busy? It will be added to a queue, and executed when a thread becomes available. In a `FixedThreadPool`, if you post work to the pool much faster than the work can be completed, the queue may grow without bounds, as the work piles up in the holding queue, using up memory without bounds. To limit the queue and apply some form of 'back pressure' instead, you can use the more configurable `ThreadPoolExecutor` (See below).
24
+
What happens if you post new work when all (e.g.) 5 threads are currently busy? It will be added to a queue, and executed when a thread becomes available. In a `FixedThreadPool`, if you post work to the pool much faster than the work can be completed, the queue may grow without bounds, as the work piles up in the holding queue, using up memory without bounds. To limit the queue and apply some form of 'back pressure' instead, you can use the more configurable `ThreadPoolExecutor` (See below).
25
25
26
26
If you'd like to base the number of threads in the pool on the number of processors available, your code can consult [Concurrent.processor_count](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ProcessorCounter.html#processor_count-instance_method).
27
27
28
28
The `FixedThreadPool` is based on the semantics used in Java for [java.util.concurrent.Executors.newFixedThreadPool(int nThreads)](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int))
29
29
30
30
## CachedThreadPool
31
31
32
-
A [CachedThreadPool](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/CachedThreadPool.html) will create as many threads as neccesary for work posted to it. If you post work to a `CachedThreadPool` when all it's existing threads are busy, it will create a new thread to execute that work, and then keep that thread cached for future work. Cached threads are reclaimed (destroyed) after they are idle for a while.
32
+
A [CachedThreadPool](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/CachedThreadPool.html) will create as many threads as necessary for work posted to it. If you post work to a `CachedThreadPool` when all its existing threads are busy, it will create a new thread to execute that work, and then keep that thread cached for future work. Cached threads are reclaimed (destroyed) after they are idle for a while.
33
33
34
34
CachedThreadPools typically improve the performance of programs that execute many short-lived asynchronous tasks.
35
35
@@ -58,18 +58,18 @@ pool = Concurrent::ThreadPoolExecutor.new(
58
58
)
59
59
~~~
60
60
61
-
If you want to provide a maximum queue size, you may also consider the `overflow_policy` -- what will happen if work is posted to a pool when the queue of waiting work has reached the maximum size? Available policies:
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:
62
62
63
63
* abort: Raise a `Concurrent::RejectedExecutionError` exception and discard the task. (default policy)
64
64
* discard: Silently discard the task and return nil as the task result.
65
-
* caller_runs: The work will be executed in the thread of the caller, instead of being given to another thread in the pool
65
+
* caller_runs: The work will be executed in the thread of the caller, instead of being given to another thread in the pool.
66
66
67
67
~~~ruby
68
68
pool =Concurrent::ThreadPoolExecutor.new(
69
69
min_threads:5,
70
70
max_threads:5,
71
71
max_queue:100,
72
-
overflow_policy::caller_runs
72
+
fallback_policy::caller_runs
73
73
)
74
74
~~~
75
75
@@ -83,14 +83,14 @@ pool = Concurrent::ThreadPoolExecutor.new(
83
83
)
84
84
~~~
85
85
86
-
Or, with a variable number of threads like a CachedThreadPool, but with a bounded queue and an overflow_policy:
86
+
Or, with a variable number of threads like a CachedThreadPool, but with a bounded queue and a fallback_policy:
87
87
88
88
~~~ruby
89
89
pool =Concurrent::ThreadPoolExecutor.new(
90
90
min_threads:3, # create 3 threads at startup
91
91
max_threads:10, # create at most 10 threads
92
92
max_queue:100, # at most 100 jobs waiting in the queue,
93
-
overflow_policy::abort
93
+
fallback_policy::abort
94
94
)
95
95
~~~
96
96
@@ -106,11 +106,11 @@ ThreadPoolExecutors with `min_threads` and `max_threads` set to different values
106
106
107
107
A running thread pool can be shutdown in an orderly or disruptive manner. Once a thread pool has been shutdown it cannot be started again.
108
108
109
-
The `shutdown` method can be used to initiate an orderly shutdown of the thread pool. All new post calls will reject the given block and immediately return false. Threads in the pool will continue to process all in-progress work and will process all tasks still in the queue.
109
+
The `shutdown` method can be used to initiate an orderly shutdown of the thread pool. All new post calls will be handled according to the `fallback_policy` (i.e. failing with a RejectedExecutionError by default). Threads in the pool will continue to process all in-progress work and will process all tasks still in the queue.
110
110
111
-
The `kill` method can be used to immediately shutdown the pool. All new post calls will reject the given block and immediately return false. Ruby's Thread.kill will be called on all threads in the pool, aborting all in-progress work. Tasks in the queue will be discarded.
111
+
The `kill` method can be used to immediately shutdown the pool. All new post calls will be handled according to the `fallback_policy`. Ruby's `Thread.kill` will be called on all threads in the pool, aborting all in-progress work. Tasks in the queue will be discarded.
112
112
113
-
The method `wait_for_termination` can be used to block and wait for pool shutdown to complete. This is useful when shutting down an application and ensuring the app doesn't exit before pool processing is complete. The method wait_for_termination will block for a maximum of the given number of seconds then return true if shutdown completed successfully or false. When the timeout value is nil the call will block indefinitely. Calling wait_for_termination on a stopped thread pool will immediately return true.
113
+
The method `wait_for_termination` can be used to block and wait for pool shutdown to complete. This is useful when shutting down an application and ensuring the app doesn't exit before pool processing is complete. The method wait_for_termination will block for a maximum of the given number of seconds then return true (if shutdown completed successfully) or false (if it was still ongoing). When the timeout value is `nil` the call will block indefinitely. Calling `wait_for_termination` on a stopped thread pool will immediately return true.
114
114
115
115
~~~ruby
116
116
# tell the pool to shutdown in an orderly fashion, allowing in progress work to complete
@@ -155,10 +155,10 @@ pool = Concurrent::ThreadPoolExecutor.new(
0 commit comments