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
+13-16Lines changed: 13 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,11 @@ If you'd like to configure a maximum number of threads, you can use the more gen
48
48
49
49
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.
50
50
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:
52
56
53
57
~~~ruby
54
58
pool =Concurrent::ThreadPoolExecutor.new(
@@ -58,12 +62,14 @@ pool = Concurrent::ThreadPoolExecutor.new(
58
62
)
59
63
~~~
60
64
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:
62
66
63
67
* abort: Raise a `Concurrent::RejectedExecutionError` exception and discard the task. (default policy)
64
68
* discard: Silently discard the task and return nil as the task result.
65
69
* caller_runs: The work will be executed in the thread of the caller, instead of being given to another thread in the pool.
66
70
71
+
For example:
72
+
67
73
~~~ruby
68
74
pool =Concurrent::ThreadPoolExecutor.new(
69
75
min_threads:5,
@@ -73,24 +79,15 @@ pool = Concurrent::ThreadPoolExecutor.new(
73
79
)
74
80
~~~
75
81
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.
87
85
88
86
~~~ruby
89
87
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
91
89
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
0 commit comments