Skip to content

Commit d650f03

Browse files
committed
JavaThreadPoolExecutor now passes the new queue specs.
1 parent 2030b8b commit d650f03

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/concurrent/java_thread_pool_executor.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class JavaThreadPoolExecutor
3030

3131
attr_reader :max_queue
3232

33+
attr_reader :overflow_policy
34+
3335
# Create a new thread pool.
3436
#
3537
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
@@ -38,23 +40,23 @@ def initialize(opts = {})
3840
@max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
3941
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
4042
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
41-
overflow_policy = opts.fetch(:overflow_policy, :abort)
43+
@overflow_policy = opts.fetch(:overflow_policy, :abort)
4244

4345
raise ArgumentError.new('max_threads must be greater than zero') if @max_length <= 0
4446
raise ArgumentError.new("#{overflow_policy} is not a valid overflow policy") unless OVERFLOW_POLICIES.keys.include?(overflow_policy)
4547

46-
if min_length == 0 && max_queue == 0
48+
if min_length == 0 && @max_queue == 0
4749
queue = java.util.concurrent.SynchronousQueue.new
48-
elsif max_queue == 0
50+
elsif @max_queue == 0
4951
queue = java.util.concurrent.LinkedBlockingQueue.new
5052
else
51-
queue = java.util.concurrent.LinkedBlockingQueue.new(max_queue)
53+
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
5254
end
5355

5456
@executor = java.util.concurrent.ThreadPoolExecutor.new(
5557
min_length, @max_length,
5658
idletime, java.util.concurrent.TimeUnit::SECONDS,
57-
queue, OVERFLOW_POLICIES[overflow_policy].new)
59+
queue, OVERFLOW_POLICIES[@overflow_policy].new)
5860
end
5961

6062
def min_length
@@ -91,7 +93,7 @@ def queue_length
9193
end
9294

9395
def remaining_capacity
94-
@executor.getQueue.remainingCapacity
96+
@max_queue == 0 ? -1 : @executor.getQueue.remainingCapacity
9597
end
9698

9799
# Is the thread pool running?
@@ -157,6 +159,7 @@ def <<(task)
157159
# thread pool is not running.
158160
def shutdown
159161
@executor.shutdown
162+
@executor.getQueue.clear
160163
return nil
161164
end
162165

@@ -166,6 +169,7 @@ def shutdown
166169
# not running.
167170
def kill
168171
@executor.shutdownNow
172+
@executor.getQueue.clear
169173
return nil
170174
end
171175

0 commit comments

Comments
 (0)