Skip to content

Commit 41a9fc7

Browse files
committed
Removed duplicate code in thread pools.
1 parent 02c28ba commit 41a9fc7

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

lib/concurrent/java_cached_thread_pool.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,27 @@ module Concurrent
77
# @!macro cached_thread_pool
88
class JavaCachedThreadPool < JavaThreadPoolExecutor
99

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-
2110
# Create a new thread pool.
2211
#
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+
#
2321
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
2422
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
2725

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
3028

3129
@executor = java.util.concurrent.ThreadPoolExecutor.new(
32-
0, @max_length,
30+
0, max_length,
3331
idletime, java.util.concurrent.TimeUnit::SECONDS,
3432
java.util.concurrent.SynchronousQueue.new,
3533
java.util.concurrent.ThreadPoolExecutor::AbortPolicy.new)

lib/concurrent/java_fixed_thread_pool.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ class JavaFixedThreadPool < JavaThreadPoolExecutor
1212
#
1313
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-
1414
def initialize(num_threads = Concurrent::processor_count)
15-
@num_threads = num_threads.to_i
16-
raise ArgumentError.new('number of threads must be greater than zero') if @num_threads < 1
15+
raise ArgumentError.new('number of threads must be greater than zero') if num_threads < 1
1716

1817
@executor = java.util.concurrent.ThreadPoolExecutor.new(
19-
@num_threads, @num_threads,
18+
num_threads, num_threads,
2019
0, java.util.concurrent.TimeUnit::SECONDS,
2120
java.util.concurrent.LinkedBlockingQueue.new,
2221
java.util.concurrent.ThreadPoolExecutor::AbortPolicy.new)

lib/concurrent/ruby_cached_thread_pool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RubyCachedThreadPool < RubyThreadPoolExecutor
1010
# @param [Hash] opts the options defining pool behavior.
1111
# @option opts [Integer] :max_threads (+DEFAULT_MAX_POOL_SIZE+) maximum number
1212
# of threads which may be created in the pool
13-
# @option opts [Integer] :idletime (+DEFAULT_THREAD_IDLETIME+) maximum
13+
# @option opts [Integer] :idletime (+DEFAULT_THREAD_IDLETIMEOUT+) maximum
1414
# number of seconds a thread may be idle before it is reclaimed
1515
#
1616
# @raise [ArgumentError] if +max_threads+ is less than or equal to zero

0 commit comments

Comments
 (0)