22
33 module Concurrent
44
5+ RejectedExecutionError = Class . new ( StandardError ) unless defined? RejectedExecutionError
6+
57 # @!macro thread_pool_executor
68 class JavaThreadPoolExecutor
79
@@ -30,31 +32,34 @@ class JavaThreadPoolExecutor
3032
3133 attr_reader :max_queue
3234
35+ attr_reader :overflow_policy
36+
3337 # Create a new thread pool.
3438 #
3539 # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
3640 def initialize ( opts = { } )
3741 min_length = opts . fetch ( :min_threads , DEFAULT_MIN_POOL_SIZE ) . to_i
38- @ 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
3943 idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
4044 @max_queue = opts . fetch ( :max_queue , DEFAULT_MAX_QUEUE_SIZE ) . to_i
41- overflow_policy = opts . fetch ( :overflow_policy , :abort )
45+ @ overflow_policy = opts . fetch ( :overflow_policy , :abort )
4246
43- raise ArgumentError . new ( 'max_threads must be greater than zero' ) if @max_length <= 0
44- 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 )
4550
46- if min_length == 0 && max_queue == 0
51+ if min_length == 0 && @ max_queue == 0
4752 queue = java . util . concurrent . SynchronousQueue . new
48- elsif max_queue == 0
53+ elsif @ max_queue == 0
4954 queue = java . util . concurrent . LinkedBlockingQueue . new
5055 else
51- queue = java . util . concurrent . LinkedBlockingQueue . new ( max_queue )
56+ queue = java . util . concurrent . LinkedBlockingQueue . new ( @ max_queue)
5257 end
5358
5459 @executor = java . util . concurrent . ThreadPoolExecutor . new (
55- min_length , @ max_length,
60+ min_length , max_length ,
5661 idletime , java . util . concurrent . TimeUnit ::SECONDS ,
57- queue , OVERFLOW_POLICIES [ overflow_policy ] . new )
62+ queue , OVERFLOW_POLICIES [ @ overflow_policy] . new )
5863 end
5964
6065 def min_length
@@ -91,14 +96,14 @@ def queue_length
9196 end
9297
9398 def remaining_capacity
94- @executor . getQueue . remainingCapacity
99+ @max_queue == 0 ? - 1 : @ executor. getQueue . remainingCapacity
95100 end
96101
97102 # Is the thread pool running?
98103 #
99104 # @return [Boolean] +true+ when running, +false+ when shutting down or shutdown
100105 def running?
101- ! ( shutdown? || terminated? )
106+ ! ( @executor . isShutdown || @executor . isTerminated || @executor . isTerminating )
102107 end
103108
104109 # Is the thread pool shutdown?
@@ -133,10 +138,14 @@ def wait_for_termination(timeout)
133138 # @raise [ArgumentError] if no task is given
134139 def post ( *args )
135140 raise ArgumentError . new ( 'no block given' ) unless block_given?
136- @executor . submit { yield ( *args ) }
137- return true
141+ if running?
142+ @executor . submit { yield ( *args ) }
143+ true
144+ else
145+ false
146+ end
138147 rescue Java ::JavaUtilConcurrent ::RejectedExecutionException => ex
139- return false
148+ raise RejectedExecutionError
140149 end
141150
142151 # Submit a task to the thread pool for asynchronous processing.
@@ -147,16 +156,15 @@ def post(*args)
147156 def <<( task )
148157 @executor . submit ( &task )
149158 rescue Java ::JavaUtilConcurrent ::RejectedExecutionException => ex
150- # do nothing
151- ensure
152- return self
159+ raise RejectedExecutionError
153160 end
154161
155162 # Begin an orderly shutdown. Tasks already in the queue will be executed,
156163 # but no new tasks will be accepted. Has no additional effect if the
157164 # thread pool is not running.
158165 def shutdown
159166 @executor . shutdown
167+ @executor . getQueue . clear
160168 return nil
161169 end
162170
@@ -166,17 +174,9 @@ def shutdown
166174 # not running.
167175 def kill
168176 @executor . shutdownNow
177+ @executor . getQueue . clear
169178 return nil
170179 end
171-
172- protected
173-
174- # Were all tasks completed before shutdown?
175- #
176- # @return [Boolean] +true+ if shutdown and all tasks completed else +false+
177- def terminated?
178- @executor . isTerminated
179- end
180180 end
181181 end
182182end
0 commit comments