@@ -146,32 +146,6 @@ def wait_for_termination(timeout)
146
146
return @stopped_event . wait ( timeout . to_i )
147
147
end
148
148
149
- # Submit a task to the thread pool for asynchronous processing.
150
- #
151
- # @param [Array] args zero or more arguments to be passed to the task
152
- #
153
- # @yield the asynchronous task to perform
154
- #
155
- # @return [Boolean] `true` if the task is queued, `false` if the thread pool
156
- # is not running
157
- #
158
- # @raise [ArgumentError] if no task is given
159
- def post ( *args , &task )
160
- raise ArgumentError . new ( 'no block given' ) unless block_given?
161
- mutex . synchronize do
162
- break false unless running?
163
- return handle_overflow ( *args , &task ) if @max_queue != 0 && @queue . length >= @max_queue
164
- @scheduled_task_count += 1
165
- @queue << [ args , task ]
166
- if Time . now . to_f - @gc_interval >= @last_gc_time
167
- prune_pool
168
- @last_gc_time = Time . now . to_f
169
- end
170
- grow_pool
171
- true
172
- end
173
- end
174
-
175
149
# Begin an orderly shutdown. Tasks already in the queue will be executed,
176
150
# but no new tasks will be accepted. Has no additional effect if the
177
151
# thread pool is not running.
@@ -205,7 +179,7 @@ def kill
205
179
# Run on task completion.
206
180
#
207
181
# @!visibility private
208
- def on_end_task # :nodoc:
182
+ def on_end_task
209
183
mutex . synchronize do
210
184
@completed_task_count += 1 #if success
211
185
break unless running?
@@ -215,7 +189,7 @@ def on_end_task # :nodoc:
215
189
# Run when a thread worker exits.
216
190
#
217
191
# @!visibility private
218
- def on_worker_exit ( worker ) # :nodoc:
192
+ def on_worker_exit ( worker )
219
193
mutex . synchronize do
220
194
@pool . delete ( worker )
221
195
if @pool . empty? && ! running?
@@ -227,13 +201,25 @@ def on_worker_exit(worker) # :nodoc:
227
201
228
202
protected
229
203
204
+ # @!visibility private
205
+ def execute ( *args , &task )
206
+ return handle_overflow ( *args , &task ) if @max_queue != 0 && @queue . length >= @max_queue
207
+ @scheduled_task_count += 1
208
+ @queue << [ args , task ]
209
+ if Time . now . to_f - @gc_interval >= @last_gc_time
210
+ prune_pool
211
+ @last_gc_time = Time . now . to_f
212
+ end
213
+ grow_pool
214
+ end
215
+
230
216
# Handler which executes the `overflow_policy` once the queue size
231
217
# reaches `max_queue`.
232
218
#
233
219
# @param [Array] args the arguments to the task which is being handled.
234
220
#
235
221
# @!visibility private
236
- def handle_overflow ( *args ) # :nodoc:
222
+ def handle_overflow ( *args )
237
223
case @overflow_policy
238
224
when :abort
239
225
raise RejectedExecutionError
@@ -253,7 +239,7 @@ def handle_overflow(*args) # :nodoc:
253
239
# too long.
254
240
#
255
241
# @!visibility private
256
- def prune_pool # :nodoc:
242
+ def prune_pool
257
243
@pool . delete_if do |worker |
258
244
worker . dead? ||
259
245
( @idletime == 0 ? false : Time . now . to_f - @idletime > worker . last_activity )
@@ -263,7 +249,7 @@ def prune_pool # :nodoc:
263
249
# Increase the size of the pool when necessary.
264
250
#
265
251
# @!visibility private
266
- def grow_pool # :nodoc:
252
+ def grow_pool
267
253
if @min_length > @pool . length
268
254
additional = @min_length - @pool . length
269
255
elsif @pool . length < @max_length && ! @queue . empty?
@@ -282,7 +268,7 @@ def grow_pool # :nodoc:
282
268
# Reclaim all threads in the pool.
283
269
#
284
270
# @!visibility private
285
- def drain_pool # :nodoc:
271
+ def drain_pool
286
272
@pool . each { |worker | worker . kill }
287
273
@pool . clear
288
274
end
@@ -292,7 +278,7 @@ def drain_pool # :nodoc:
292
278
# @return [Thread] the new thread.
293
279
#
294
280
# @!visibility private
295
- def create_worker_thread # :nodoc:
281
+ def create_worker_thread
296
282
wrkr = RubyThreadPoolWorker . new ( @queue , self )
297
283
Thread . new ( wrkr , self ) do |worker , parent |
298
284
Thread . current . abort_on_exception = false
0 commit comments