@@ -11,20 +11,20 @@ class RubyThreadPoolExecutor
11
11
include RubyExecutor
12
12
13
13
# Default maximum number of threads that will be created in the pool.
14
- DEFAULT_MAX_POOL_SIZE = 2 **15 # 32768
14
+ DEFAULT_MAX_POOL_SIZE = 2 **15 # 32768
15
15
16
16
# Default minimum number of threads that will be retained in the pool.
17
- DEFAULT_MIN_POOL_SIZE = 0
17
+ DEFAULT_MIN_POOL_SIZE = 0
18
18
19
19
# Default maximum number of tasks that may be added to the task queue.
20
- DEFAULT_MAX_QUEUE_SIZE = 0
20
+ DEFAULT_MAX_QUEUE_SIZE = 0
21
21
22
22
# Default maximum number of seconds a thread in the pool may remain idle
23
23
# before being reclaimed.
24
24
DEFAULT_THREAD_IDLETIMEOUT = 60
25
25
26
26
# The set of possible overflow policies that may be set at thread pool creation.
27
- OVERFLOW_POLICIES = [ :abort , :discard , :caller_runs ]
27
+ OVERFLOW_POLICIES = [ :abort , :discard , :caller_runs ]
28
28
29
29
# The maximum number of threads that may be created in the pool.
30
30
attr_reader :max_length
@@ -77,10 +77,10 @@ class RubyThreadPoolExecutor
77
77
#
78
78
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
79
79
def initialize ( opts = { } )
80
- @min_length = opts . fetch ( :min_threads , DEFAULT_MIN_POOL_SIZE ) . to_i
81
- @max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
82
- @idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
83
- @max_queue = opts . fetch ( :max_queue , DEFAULT_MAX_QUEUE_SIZE ) . to_i
80
+ @min_length = opts . fetch ( :min_threads , DEFAULT_MIN_POOL_SIZE ) . to_i
81
+ @max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
82
+ @idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
83
+ @max_queue = opts . fetch ( :max_queue , DEFAULT_MAX_QUEUE_SIZE ) . to_i
84
84
@overflow_policy = opts . fetch ( :overflow_policy , :abort )
85
85
86
86
raise ArgumentError . new ( 'max_threads must be greater than zero' ) if @max_length <= 0
@@ -90,13 +90,13 @@ def initialize(opts = {})
90
90
91
91
init_executor
92
92
93
- @pool = [ ]
94
- @queue = Queue . new
93
+ @pool = [ ]
94
+ @queue = Queue . new
95
95
@scheduled_task_count = 0
96
96
@completed_task_count = 0
97
- @largest_length = 0
97
+ @largest_length = 0
98
98
99
- @gc_interval = opts . fetch ( :gc_interval , 1 ) . to_i # undocumented
99
+ @gc_interval = opts . fetch ( :gc_interval , 1 ) . to_i # undocumented
100
100
@last_gc_time = Time . now . to_f - [ 1.0 , ( @gc_interval * 2.0 ) ] . max
101
101
end
102
102
@@ -109,15 +109,16 @@ def can_overflow?
109
109
#
110
110
# @return [Integer] the length
111
111
def length
112
- mutex . synchronize { running? ? @pool . length : 0 }
112
+ mutex . synchronize { running? ? @pool . length : 0 }
113
113
end
114
+
114
115
alias_method :current_length , :length
115
116
116
117
# The number of tasks in the queue awaiting execution.
117
118
#
118
119
# @return [Integer] the queue_length
119
120
def queue_length
120
- mutex . synchronize { running? ? @queue . length : 0 }
121
+ mutex . synchronize { running? ? @queue . length : 0 }
121
122
end
122
123
123
124
# Number of tasks that may be enqueued before reaching `max_queue` and rejecting
@@ -152,7 +153,7 @@ def on_end_task
152
153
def on_worker_exit ( worker )
153
154
mutex . synchronize do
154
155
@pool . delete ( worker )
155
- if @pool . empty? && ! running?
156
+ if @pool . empty? && !running?
156
157
stop_event . set
157
158
stopped_event . set
158
159
end
@@ -177,7 +178,7 @@ def shutdown_execution
177
178
if @pool . empty?
178
179
stopped_event . set
179
180
else
180
- @pool . length . times { @queue << :stop }
181
+ @pool . length . times { @queue << :stop }
181
182
end
182
183
end
183
184
@@ -196,7 +197,7 @@ def kill_execution
196
197
# @!visibility private
197
198
def ensure_capacity?
198
199
additional = 0
199
- capacity = true
200
+ capacity = true
200
201
201
202
if @pool . size < @min_length
202
203
additional = @min_length - @pool . size
@@ -254,10 +255,11 @@ def handle_overflow(*args)
254
255
# @!visibility private
255
256
def prune_pool
256
257
if Time . now . to_f - @gc_interval >= @last_gc_time
257
- @pool . delete_if do |worker |
258
- worker . dead? ||
259
- ( @idletime == 0 ? false : Time . now . to_f - @idletime > worker . last_activity )
260
- end
258
+ @pool . delete_if { |worker | worker . dead? }
259
+ # send :stop for each thread over idletime
260
+ @pool .
261
+ select { |worker | @idletime != 0 && Time . now . to_f - @idletime > worker . last_activity } .
262
+ each { @queue << :stop }
261
263
@last_gc_time = Time . now . to_f
262
264
end
263
265
end
@@ -266,7 +268,7 @@ def prune_pool
266
268
#
267
269
# @!visibility private
268
270
def drain_pool
269
- @pool . each { |worker | worker . kill }
271
+ @pool . each { |worker | worker . kill }
270
272
@pool . clear
271
273
end
272
274
0 commit comments