@@ -20,26 +20,14 @@ class JavaThreadPoolExecutor
2020 # before being reclaimed.
2121 DEFAULT_THREAD_IDLETIMEOUT = 60
2222
23- # The set of possible overflow policies that may be set at thread pool creation.
24- OVERFLOW_POLICIES = {
25- abort : java . util . concurrent . ThreadPoolExecutor ::AbortPolicy ,
26- discard : java . util . concurrent . ThreadPoolExecutor ::DiscardPolicy ,
27- caller_runs : java . util . concurrent . ThreadPoolExecutor ::CallerRunsPolicy
28- } . freeze
29-
3023 # The maximum number of threads that may be created in the pool.
3124 attr_reader :max_length
3225
3326 # The maximum number of tasks that may be waiting in the work queue at any one time.
3427 # When the queue size reaches `max_queue` subsequent tasks will be rejected in
35- # accordance with the configured `overflow_policy `.
28+ # accordance with the configured `fallback_policy `.
3629 attr_reader :max_queue
3730
38- # The policy defining how rejected tasks (tasks received once the queue size reaches
39- # the configured `max_queue`) are handled. Must be one of the values specified in
40- # `OVERFLOW_POLICIES`.
41- attr_reader :overflow_policy
42-
4331 # Create a new thread pool.
4432 #
4533 # @param [Hash] opts the options which configure the thread pool
@@ -52,27 +40,28 @@ class JavaThreadPoolExecutor
5240 # number of seconds a thread may be idle before being reclaimed
5341 # @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum
5442 # number of tasks allowed in the work queue at any one time; a value of
55- # zero means the queue may grow without bounnd
56- # @option opts [Symbol] :overflow_policy (:abort) the policy for handling new
57- # tasks that are received when the queue size has reached `max_queue`
43+ # zero means the queue may grow without bound
44+ # @option opts [Symbol] :fallback_policy (:abort) the policy for handling new
45+ # tasks that are received when the queue size has reached
46+ # `max_queue` or the executir has shut down
5847 #
5948 # @raise [ArgumentError] if `:max_threads` is less than one
6049 # @raise [ArgumentError] if `:min_threads` is less than zero
61- # @raise [ArgumentError] if `:overflow_policy ` is not one of the values specified
62- # in `OVERFLOW_POLICIES `
50+ # @raise [ArgumentError] if `:fallback_policy ` is not one of the values specified
51+ # in `FALLBACK_POLICIES `
6352 #
6453 # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
6554 def initialize ( opts = { } )
6655 min_length = opts . fetch ( :min_threads , DEFAULT_MIN_POOL_SIZE ) . to_i
6756 max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
6857 idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
6958 @max_queue = opts . fetch ( :max_queue , DEFAULT_MAX_QUEUE_SIZE ) . to_i
70- @overflow_policy = opts . fetch ( :overflow_policy , :abort )
59+ @fallback_policy = opts . fetch ( :fallback_policy , opts . fetch ( : overflow_policy, :abort ) )
7160
7261 raise ArgumentError . new ( 'max_threads must be greater than zero' ) if max_length <= 0
7362 raise ArgumentError . new ( 'min_threads cannot be less than zero' ) if min_length < 0
7463 raise ArgumentError . new ( 'min_threads cannot be more than max_threads' ) if min_length > max_length
75- raise ArgumentError . new ( "#{ @overflow_policy } is not a valid overflow policy" ) unless OVERFLOW_POLICIES . keys . include? ( @overflow_policy )
64+ raise ArgumentError . new ( "#{ fallback_policy } is not a valid fallback policy" ) unless FALLBACK_POLICIES . include? ( @fallback_policy )
7665
7766 if @max_queue == 0
7867 queue = java . util . concurrent . LinkedBlockingQueue . new
@@ -83,7 +72,7 @@ def initialize(opts = {})
8372 @executor = java . util . concurrent . ThreadPoolExecutor . new (
8473 min_length , max_length ,
8574 idletime , java . util . concurrent . TimeUnit ::SECONDS ,
86- queue , OVERFLOW_POLICIES [ @overflow_policy ] . new )
75+ queue , FALLBACK_POLICIES [ @fallback_policy ] . new )
8776
8877 set_shutdown_hook
8978 end
0 commit comments