|
1 |
| -require 'concurrent/executor/ruby_fixed_thread_pool' |
| 1 | +require 'concurrent/utility/engine' |
| 2 | +require 'concurrent/executor/thread_pool_executor' |
2 | 3 |
|
3 | 4 | module Concurrent
|
4 | 5 |
|
5 |
| - if Concurrent.on_jruby? |
6 |
| - require 'concurrent/executor/java_fixed_thread_pool' |
7 |
| - end |
8 |
| - |
9 |
| - FixedThreadPoolImplementation = case |
10 |
| - when Concurrent.on_jruby? |
11 |
| - JavaFixedThreadPool |
12 |
| - else |
13 |
| - RubyFixedThreadPool |
14 |
| - end |
15 |
| - private_constant :FixedThreadPoolImplementation |
16 |
| - |
17 | 6 | # @!macro [new] thread_pool_executor_constant_default_max_pool_size
|
18 | 7 | # Default maximum number of threads that will be created in the pool.
|
19 | 8 |
|
@@ -119,35 +108,7 @@ module Concurrent
|
119 | 108 |
|
120 | 109 |
|
121 | 110 |
|
122 |
| - |
123 |
| - # @!macro [new] fixed_thread_pool_method_initialize |
124 |
| - # |
125 |
| - # Create a new thread pool. |
126 |
| - # |
127 |
| - # @param [Integer] num_threads the number of threads to allocate |
128 |
| - # @param [Hash] opts the options defining pool behavior. |
129 |
| - # @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy |
130 |
| - # |
131 |
| - # @raise [ArgumentError] if `num_threads` is less than or equal to zero |
132 |
| - # @raise [ArgumentError] if `fallback_policy` is not a known policy |
133 |
| - # |
134 |
| - # @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int- |
135 |
| - |
136 |
| - |
137 |
| - |
138 |
| - |
139 |
| - |
140 |
| - # @!macro [attach] fixed_thread_pool |
141 |
| - # |
142 |
| - # A thread pool with a set number of threads. The number of threads in the pool |
143 |
| - # is set on construction and remains constant. When all threads are busy new |
144 |
| - # tasks `#post` to the thread pool are enqueued until a thread becomes available. |
145 |
| - # Should a thread crash for any reason the thread will immediately be removed |
146 |
| - # from the pool and replaced. |
147 |
| - # |
148 |
| - # The API and behavior of this class are based on Java's `FixedThreadPool` |
149 |
| - # |
150 |
| - # @!macro [attach] thread_pool_options |
| 111 | + # @!macro [new] thread_pool_options |
151 | 112 | #
|
152 | 113 | # **Thread Pool Options**
|
153 | 114 | #
|
@@ -203,11 +164,43 @@ module Concurrent
|
203 | 164 | # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
|
204 | 165 | # @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface
|
205 | 166 | # @see http://ruby-doc.org//core-2.2.0/Kernel.html#method-i-at_exit Kernel#at_exit
|
206 |
| - # |
207 |
| - # @!macro thread_pool_executor_public_api |
208 |
| - class FixedThreadPool < FixedThreadPoolImplementation |
209 | 167 |
|
210 |
| - # @!method initialize(num_threads, opts = {}) |
211 |
| - # @!macro fixed_thread_pool_method_initialize |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + # @!macro [attach] fixed_thread_pool |
| 173 | + # |
| 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 |
| 176 | + # tasks `#post` to the thread pool are enqueued until a thread becomes available. |
| 177 | + # Should a thread crash for any reason the thread will immediately be removed |
| 178 | + # from the pool and replaced. |
| 179 | + # |
| 180 | + # The API and behavior of this class are based on Java's `FixedThreadPool` |
| 181 | + # |
| 182 | + # @!macro thread_pool_options |
| 183 | + class FixedThreadPool < ThreadPoolExecutor |
| 184 | + |
| 185 | + # @!macro [attach] fixed_thread_pool_method_initialize |
| 186 | + # |
| 187 | + # Create a new thread pool. |
| 188 | + # |
| 189 | + # @param [Integer] num_threads the number of threads to allocate |
| 190 | + # @param [Hash] opts the options defining pool behavior. |
| 191 | + # @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy |
| 192 | + # |
| 193 | + # @raise [ArgumentError] if `num_threads` is less than or equal to zero |
| 194 | + # @raise [ArgumentError] if `fallback_policy` is not a known policy |
| 195 | + # |
| 196 | + # @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int- |
| 197 | + def initialize(num_threads, opts = {}) |
| 198 | + raise ArgumentError.new('number of threads must be greater than zero') if num_threads.to_i < 1 |
| 199 | + defaults = { max_queue: DEFAULT_MAX_QUEUE_SIZE, |
| 200 | + idletime: DEFAULT_THREAD_IDLETIMEOUT } |
| 201 | + overrides = { min_threads: num_threads, |
| 202 | + max_threads: num_threads } |
| 203 | + super(defaults.merge(opts).merge(overrides)) |
| 204 | + end |
212 | 205 | end
|
213 | 206 | end
|
0 commit comments