@@ -7,29 +7,27 @@ module Concurrent
7
7
# @!macro cached_thread_pool
8
8
class JavaCachedThreadPool < JavaThreadPoolExecutor
9
9
10
- # The maximum number of threads that may be created in the pool
11
- # (unless overridden during construction).
12
- DEFAULT_MAX_POOL_SIZE = java . lang . Integer ::MAX_VALUE # 2147483647
13
-
14
- # The maximum number of seconds a thread in the pool may remain idle before
15
- # being reclaimed (unless overridden during construction).
16
- DEFAULT_THREAD_IDLETIME = 60
17
-
18
- # The maximum number of threads that may be created in the pool.
19
- attr_reader :max_length
20
-
21
10
# Create a new thread pool.
22
11
#
12
+ # @param [Hash] opts the options defining pool behavior.
13
+ # @option opts [Integer] :max_threads (+DEFAULT_MAX_POOL_SIZE+) maximum number
14
+ # of threads which may be created in the pool
15
+ # @option opts [Integer] :idletime (+DEFAULT_THREAD_IDLETIMEOUT+) maximum
16
+ # number of seconds a thread may be idle before it is reclaimed
17
+ #
18
+ # @raise [ArgumentError] if +max_threads+ is less than or equal to zero
19
+ # @raise [ArgumentError] if +thread_idletime+ is less than or equal to zero
20
+ #
23
21
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
24
22
def initialize ( opts = { } )
25
- idletime = ( opts [ :thread_idletime ] || opts [ :idletime ] || DEFAULT_THREAD_IDLETIME ) . to_i
26
- raise ArgumentError . new ( ' idletime must be greater than zero' ) if idletime <= 0
23
+ max_length = opts . fetch ( :max_threads , DEFAULT_MAX_POOL_SIZE ) . to_i
24
+ idletime = opts . fetch ( : idletime, DEFAULT_THREAD_IDLETIMEOUT ) . to_i
27
25
28
- @max_length = opts [ :max_threads ] || opts [ :max ] || DEFAULT_MAX_POOL_SIZE
29
- raise ArgumentError . new ( 'maximum_number of threads must be greater than zero' ) if @ max_length <= 0
26
+ raise ArgumentError . new ( 'idletime must be greater than zero' ) if idletime <= 0
27
+ raise ArgumentError . new ( 'max_threads must be greater than zero' ) if max_length <= 0
30
28
31
29
@executor = java . util . concurrent . ThreadPoolExecutor . new (
32
- 0 , @ max_length,
30
+ 0 , max_length ,
33
31
idletime , java . util . concurrent . TimeUnit ::SECONDS ,
34
32
java . util . concurrent . SynchronousQueue . new ,
35
33
java . util . concurrent . ThreadPoolExecutor ::AbortPolicy . new )
0 commit comments