@@ -30,6 +30,8 @@ class JavaThreadPoolExecutor
30
30
31
31
attr_reader :max_queue
32
32
33
+ attr_reader :overflow_policy
34
+
33
35
# Create a new thread pool.
34
36
#
35
37
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
@@ -38,23 +40,23 @@ def initialize(opts = {})
38
40
@max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
39
41
idletime = opts . fetch ( :idletime , DEFAULT_THREAD_IDLETIMEOUT ) . to_i
40
42
@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 )
42
44
43
45
raise ArgumentError . new ( 'max_threads must be greater than zero' ) if @max_length <= 0
44
46
raise ArgumentError . new ( "#{ overflow_policy } is not a valid overflow policy" ) unless OVERFLOW_POLICIES . keys . include? ( overflow_policy )
45
47
46
- if min_length == 0 && max_queue == 0
48
+ if min_length == 0 && @ max_queue == 0
47
49
queue = java . util . concurrent . SynchronousQueue . new
48
- elsif max_queue == 0
50
+ elsif @ max_queue == 0
49
51
queue = java . util . concurrent . LinkedBlockingQueue . new
50
52
else
51
- queue = java . util . concurrent . LinkedBlockingQueue . new ( max_queue )
53
+ queue = java . util . concurrent . LinkedBlockingQueue . new ( @ max_queue)
52
54
end
53
55
54
56
@executor = java . util . concurrent . ThreadPoolExecutor . new (
55
57
min_length , @max_length ,
56
58
idletime , java . util . concurrent . TimeUnit ::SECONDS ,
57
- queue , OVERFLOW_POLICIES [ overflow_policy ] . new )
59
+ queue , OVERFLOW_POLICIES [ @ overflow_policy] . new )
58
60
end
59
61
60
62
def min_length
@@ -91,7 +93,7 @@ def queue_length
91
93
end
92
94
93
95
def remaining_capacity
94
- @executor . getQueue . remainingCapacity
96
+ @max_queue == 0 ? - 1 : @ executor. getQueue . remainingCapacity
95
97
end
96
98
97
99
# Is the thread pool running?
@@ -157,6 +159,7 @@ def <<(task)
157
159
# thread pool is not running.
158
160
def shutdown
159
161
@executor . shutdown
162
+ @executor . getQueue . clear
160
163
return nil
161
164
end
162
165
@@ -166,6 +169,7 @@ def shutdown
166
169
# not running.
167
170
def kill
168
171
@executor . shutdownNow
172
+ @executor . getQueue . clear
169
173
return nil
170
174
end
171
175
0 commit comments