22
33 module Concurrent
44
5+ RejectedExecutionError = Class . new ( StandardError ) unless defined? RejectedExecutionError
6+
57 # @!macro thread_pool_executor
68 class JavaThreadPoolExecutor
79
@@ -37,13 +39,14 @@ class JavaThreadPoolExecutor
3739 # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
3840 def initialize ( opts = { } )
3941 min_length = opts . fetch ( :min_threads , DEFAULT_MIN_POOL_SIZE ) . to_i
40- @ max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
42+ max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
4143 idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
4244 @max_queue = opts . fetch ( :max_queue , DEFAULT_MAX_QUEUE_SIZE ) . to_i
4345 @overflow_policy = opts . fetch ( :overflow_policy , :abort )
4446
45- raise ArgumentError . new ( 'max_threads must be greater than zero' ) if @max_length <= 0
46- raise ArgumentError . new ( "#{ overflow_policy } is not a valid overflow policy" ) unless OVERFLOW_POLICIES . keys . include? ( overflow_policy )
47+ raise ArgumentError . new ( 'max_threads must be greater than zero' ) if max_length <= 0
48+ raise ArgumentError . new ( 'min_threads cannot be less than zero' ) if min_length < 0
49+ raise ArgumentError . new ( "#{ @overflow_policy } is not a valid overflow policy" ) unless OVERFLOW_POLICIES . keys . include? ( @overflow_policy )
4750
4851 if min_length == 0 && @max_queue == 0
4952 queue = java . util . concurrent . SynchronousQueue . new
@@ -54,7 +57,7 @@ def initialize(opts = {})
5457 end
5558
5659 @executor = java . util . concurrent . ThreadPoolExecutor . new (
57- min_length , @ max_length,
60+ min_length , max_length ,
5861 idletime , java . util . concurrent . TimeUnit ::SECONDS ,
5962 queue , OVERFLOW_POLICIES [ @overflow_policy ] . new )
6063 end
@@ -100,7 +103,7 @@ def remaining_capacity
100103 #
101104 # @return [Boolean] +true+ when running, +false+ when shutting down or shutdown
102105 def running?
103- ! ( shutdown? || terminated? )
106+ ! ( @executor . isShutdown || @executor . isTerminated || @executor . isTerminating )
104107 end
105108
106109 # Is the thread pool shutdown?
@@ -135,10 +138,14 @@ def wait_for_termination(timeout)
135138 # @raise [ArgumentError] if no task is given
136139 def post ( *args )
137140 raise ArgumentError . new ( 'no block given' ) unless block_given?
138- @executor . submit { yield ( *args ) }
139- return true
141+ if running?
142+ @executor . submit { yield ( *args ) }
143+ true
144+ else
145+ false
146+ end
140147 rescue Java ::JavaUtilConcurrent ::RejectedExecutionException => ex
141- return false
148+ raise RejectedExecutionError
142149 end
143150
144151 # Submit a task to the thread pool for asynchronous processing.
@@ -149,17 +156,15 @@ def post(*args)
149156 def <<( task )
150157 @executor . submit ( &task )
151158 rescue Java ::JavaUtilConcurrent ::RejectedExecutionException => ex
152- # do nothing
153- ensure
154- return self
159+ raise RejectedExecutionError
155160 end
156161
157162 # Begin an orderly shutdown. Tasks already in the queue will be executed,
158163 # but no new tasks will be accepted. Has no additional effect if the
159164 # thread pool is not running.
160165 def shutdown
161- @executor . getQueue . clear
162166 @executor . shutdown
167+ @executor . getQueue . clear
163168 return nil
164169 end
165170
@@ -168,19 +173,10 @@ def shutdown
168173 # will be accepted. Has no additional effect if the thread pool is
169174 # not running.
170175 def kill
171- @executor . getQueue . clear
172176 @executor . shutdownNow
177+ @executor . getQueue . clear
173178 return nil
174179 end
175-
176- protected
177-
178- # Were all tasks completed before shutdown?
179- #
180- # @return [Boolean] +true+ if shutdown and all tasks completed else +false+
181- def terminated?
182- @executor . isTerminated
183- end
184180 end
185181 end
186182end
0 commit comments