Skip to content

Commit 8a1f12c

Browse files
committed
Better documentation of ThreadPoolExecutor.
1 parent 031c29c commit 8a1f12c

File tree

5 files changed

+212
-126
lines changed

5 files changed

+212
-126
lines changed

lib/concurrent/executor/executor_service.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,18 @@ def serialized?
9595
end
9696
end
9797

98-
# @api private
98+
# @!visibility private
9999
class AbstractExecutorService < Synchronization::Object
100100
include ExecutorService
101101

102102
# The set of possible fallback policies that may be set at thread pool creation.
103103
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
104104

105+
# @!macro [attach] executor_service_attr_reader_fallback_policy
106+
# @return [Symbol] The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.
105107
attr_reader :fallback_policy
106108

109+
# Create a new thread pool.
107110
def initialize(*args, &block)
108111
super(&nil)
109112
synchronize { ns_initialize(*args, &block) }
@@ -170,10 +173,22 @@ def shutdown?
170173
synchronize { ns_shutdown? }
171174
end
172175

176+
# @!macro [attach] executor_service_method_auto_terminate_question
177+
#
178+
# Is the executor auto-terminate when the application exits?
179+
#
180+
# @return [Boolean] `true` when auto-termination is enabled else `false`.
173181
def auto_terminate?
174182
synchronize { ns_auto_terminate? }
175183
end
176184

185+
# @!macro [attach] executor_service_method_auto_terminate_setter
186+
#
187+
# Set the auto-terminate behavior for this executor.
188+
#
189+
# @param [Boolean] value The new auto-terminate value to set for this executor.
190+
#
191+
# @return [Boolean] `true` when auto-termination is enabled else `false`.
177192
def auto_terminate=(value)
178193
synchronize { self.ns_auto_terminate = value }
179194
end

lib/concurrent/executor/fixed_thread_pool.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ module Concurrent
2424
#
2525
# The API and behavior of this class are based on Java's `FixedThreadPool`
2626
#
27-
# @see Concurrent::RubyFixedThreadPool
28-
# @see Concurrent::JavaFixedThreadPool
29-
#
3027
# @!macro [attach] thread_pool_options
3128
#
3229
# Thread pools support several configuration options:
@@ -77,12 +74,6 @@ module Concurrent
7774
# @note Failure to properly shutdown a thread pool can lead to unpredictable results.
7875
# Please read *Shutting Down Thread Pools* for more information.
7976
#
80-
# @note When running on the JVM (JRuby) this class will inherit from `JavaFixedThreadPool`.
81-
# On all other platforms it will inherit from `RubyFixedThreadPool`.
82-
#
83-
# @see Concurrent::RubyFixedThreadPool
84-
# @see Concurrent::JavaFixedThreadPool
85-
#
8677
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Java Tutorials: Thread Pools
8778
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
8879
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface

lib/concurrent/executor/java_thread_pool_executor.rb

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,28 @@ module Concurrent
55

66
# @!macro thread_pool_executor
77
# @!macro thread_pool_options
8-
# @api private
8+
# @!visibility private
99
class JavaThreadPoolExecutor < JavaExecutorService
1010

11-
# Default maximum number of threads that will be created in the pool.
11+
# @!macro thread_pool_executor_constant_default_max_pool_size
1212
DEFAULT_MAX_POOL_SIZE = java.lang.Integer::MAX_VALUE # 2147483647
1313

14-
# Default minimum number of threads that will be retained in the pool.
14+
# @!macro thread_pool_executor_constant_default_min_pool_size
1515
DEFAULT_MIN_POOL_SIZE = 0
1616

17-
# Default maximum number of tasks that may be added to the task queue.
17+
# @!macro thread_pool_executor_constant_default_max_queue_size
1818
DEFAULT_MAX_QUEUE_SIZE = 0
1919

20-
# Default maximum number of seconds a thread in the pool may remain idle
21-
# before being reclaimed.
20+
# @!macro thread_pool_executor_constant_default_thread_timeout
2221
DEFAULT_THREAD_IDLETIMEOUT = 60
2322

24-
# The maximum number of threads that may be created in the pool.
23+
# @!macro thread_pool_executor_attr_reader_max_length
2524
attr_reader :max_length
2625

27-
# The maximum number of tasks that may be waiting in the work queue at any one time.
28-
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
29-
# accordance with the configured `fallback_policy`.
26+
# @!macro thread_pool_executor_attr_reader_max_queue
3027
attr_reader :max_queue
3128

32-
# Create a new thread pool.
33-
#
34-
# @param [Hash] opts the options which configure the thread pool
35-
#
36-
# @option opts [Integer] :max_threads (DEFAULT_MAX_POOL_SIZE) the maximum
37-
# number of threads to be created
38-
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) the minimum
39-
# number of threads to be retained
40-
# @option opts [Integer] :idletime (DEFAULT_THREAD_IDLETIMEOUT) the maximum
41-
# number of seconds a thread may be idle before being reclaimed
42-
# @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum
43-
# number of tasks allowed in the work queue at any one time; a value of
44-
# zero means the queue may grow without bound
45-
# @option opts [Symbol] :fallback_policy (:abort) the policy for handling new
46-
# tasks that are received when the queue size has reached
47-
# `max_queue` or the executir has shut down
48-
#
49-
# @raise [ArgumentError] if `:max_threads` is less than one
50-
# @raise [ArgumentError] if `:min_threads` is less than zero
51-
# @raise [ArgumentError] if `:fallback_policy` is not one of the values specified
52-
# in `FALLBACK_POLICIES`
53-
#
54-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
29+
# @!macro thread_pool_executor_method_initialize
5530
def initialize(opts = {})
5631
super(opts)
5732
end
@@ -61,73 +36,52 @@ def can_overflow?
6136
@max_queue != 0
6237
end
6338

64-
# The minimum number of threads that may be retained in the pool.
65-
#
66-
# @return [Integer] the min_length
39+
# @!macro thread_pool_executor_attr_reader_min_length
6740
def min_length
6841
@executor.getCorePoolSize
6942
end
7043

71-
# The maximum number of threads that may be created in the pool.
72-
#
73-
# @return [Integer] the max_length
44+
# @!macro thread_pool_executor_attr_reader_max_length
7445
def max_length
7546
@executor.getMaximumPoolSize
7647
end
7748

78-
# The number of threads currently in the pool.
79-
#
80-
# @return [Integer] the length
49+
# @!macro thread_pool_executor_attr_reader_length
8150
def length
8251
@executor.getPoolSize
8352
end
8453

85-
# The largest number of threads that have been created in the pool since construction.
86-
#
87-
# @return [Integer] the largest_length
54+
# @!macro thread_pool_executor_attr_reader_largest_length
8855
def largest_length
8956
@executor.getLargestPoolSize
9057
end
9158

92-
# The number of tasks that have been scheduled for execution on the pool since construction.
93-
#
94-
# @return [Integer] the scheduled_task_count
59+
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
9560
def scheduled_task_count
9661
@executor.getTaskCount
9762
end
9863

99-
# The number of tasks that have been completed by the pool since construction.
100-
#
101-
# @return [Integer] the completed_task_count
64+
# @!macro thread_pool_executor_attr_reader_completed_task_count
10265
def completed_task_count
10366
@executor.getCompletedTaskCount
10467
end
10568

106-
# The number of seconds that a thread may be idle before being reclaimed.
107-
#
108-
# @return [Integer] the idletime
69+
# @!macro thread_pool_executor_attr_reader_idletime
10970
def idletime
11071
@executor.getKeepAliveTime(java.util.concurrent.TimeUnit::SECONDS)
11172
end
11273

113-
# The number of tasks in the queue awaiting execution.
114-
#
115-
# @return [Integer] the queue_length
74+
# @!macro thread_pool_executor_attr_reader_queue_length
11675
def queue_length
11776
@executor.getQueue.size
11877
end
11978

120-
# Number of tasks that may be enqueued before reaching `max_queue` and rejecting
121-
# new tasks. A value of -1 indicates that the queue may grow without bound.
122-
#
123-
# @return [Integer] the remaining_capacity
79+
# @!macro thread_pool_executor_attr_reader_remaining_capacity
12480
def remaining_capacity
12581
@max_queue == 0 ? -1 : @executor.getQueue.remainingCapacity
12682
end
12783

128-
# Is the thread pool running?
129-
#
130-
# @return [Boolean] `true` when running, `false` when shutting down or shutdown
84+
# @!macro executor_service_method_running_question
13185
def running?
13286
super && !@executor.isTerminating
13387
end

lib/concurrent/executor/ruby_thread_pool_executor.rb

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,43 @@ module Concurrent
99

1010
# @!macro thread_pool_executor
1111
# @!macro thread_pool_options
12-
# @api private
12+
# @!visibility private
1313
class RubyThreadPoolExecutor < RubyExecutorService
1414

15-
# Default maximum number of threads that will be created in the pool.
15+
# @!macro thread_pool_executor_constant_default_max_pool_size
1616
DEFAULT_MAX_POOL_SIZE = 2_147_483_647 # java.lang.Integer::MAX_VALUE
1717

18-
# Default minimum number of threads that will be retained in the pool.
18+
# @!macro thread_pool_executor_constant_default_min_pool_size
1919
DEFAULT_MIN_POOL_SIZE = 0
2020

21-
# Default maximum number of tasks that may be added to the task queue.
21+
# @!macro thread_pool_executor_constant_default_max_queue_size
2222
DEFAULT_MAX_QUEUE_SIZE = 0
2323

24-
# Default maximum number of seconds a thread in the pool may remain idle
25-
# before being reclaimed.
24+
# @!macro thread_pool_executor_constant_default_thread_timeout
2625
DEFAULT_THREAD_IDLETIMEOUT = 60
2726

28-
# The maximum number of threads that may be created in the pool.
27+
# @!macro thread_pool_executor_attr_reader_max_length
2928
attr_reader :max_length
3029

31-
# The minimum number of threads that may be retained in the pool.
30+
# @!macro thread_pool_executor_attr_reader_min_length
3231
attr_reader :min_length
3332

34-
# The largest number of threads that have been created in the pool since construction.
33+
# @!macro thread_pool_executor_attr_reader_largest_length
3534
attr_reader :largest_length
3635

37-
# The number of tasks that have been scheduled for execution on the pool since construction.
36+
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
3837
attr_reader :scheduled_task_count
3938

40-
# The number of tasks that have been completed by the pool since construction.
39+
# @!macro thread_pool_executor_attr_reader_completed_task_count
4140
attr_reader :completed_task_count
4241

43-
# The number of seconds that a thread may be idle before being reclaimed.
42+
# @!macro thread_pool_executor_attr_reader_idletime
4443
attr_reader :idletime
4544

46-
# The maximum number of tasks that may be waiting in the work queue at any one time.
47-
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
48-
# accordance with the configured `fallback_policy`.
45+
# @!macro thread_pool_executor_attr_reader_max_queue
4946
attr_reader :max_queue
5047

51-
# Create a new thread pool.
52-
#
53-
# @param [Hash] opts the options which configure the thread pool
54-
#
55-
# @option opts [Integer] :max_threads (DEFAULT_MAX_POOL_SIZE) the maximum
56-
# number of threads to be created
57-
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) the minimum
58-
# number of threads to be retained
59-
# @option opts [Integer] :idletime (DEFAULT_THREAD_IDLETIMEOUT) the maximum
60-
# number of seconds a thread may be idle before being reclaimed
61-
# @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum
62-
# number of tasks allowed in the work queue at any one time; a value of
63-
# zero means the queue may grow without bound
64-
# @option opts [Symbol] :fallback_policy (:abort) the policy for handling new
65-
# tasks that are received when the queue size has reached
66-
# `max_queue` or the executor has shut down
67-
#
68-
# @raise [ArgumentError] if `:max_threads` is less than one
69-
# @raise [ArgumentError] if `:min_threads` is less than zero
70-
# @raise [ArgumentError] if `:fallback_policy` is not one of the values specified
71-
# in `FALLBACK_POLICIES`
72-
#
73-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
48+
# @!macro thread_pool_executor_method_initialize
7449
def initialize(opts = {})
7550
super(opts)
7651
end
@@ -80,24 +55,17 @@ def can_overflow?
8055
synchronize { ns_limited_queue? }
8156
end
8257

83-
# The number of threads currently in the pool.
84-
#
85-
# @return [Integer] the length
58+
# @!macro thread_pool_executor_attr_reader_length
8659
def length
8760
synchronize { @pool.length }
8861
end
8962

90-
# The number of tasks in the queue awaiting execution.
91-
#
92-
# @return [Integer] the queue_length
63+
# @!macro thread_pool_executor_attr_reader_queue_length
9364
def queue_length
9465
synchronize { @queue.length }
9566
end
9667

97-
# Number of tasks that may be enqueued before reaching `max_queue` and rejecting
98-
# new tasks. A value of -1 indicates that the queue may grow without bound.
99-
#
100-
# @return [Integer] the remaining_capacity
68+
# @!macro thread_pool_executor_attr_reader_remaining_capacity
10169
def remaining_capacity
10270
synchronize do
10371
if ns_limited_queue?
@@ -108,22 +76,22 @@ def remaining_capacity
10876
end
10977
end
11078

111-
# @api private
79+
# @!visibility private
11280
def remove_busy_worker(worker)
11381
synchronize { ns_remove_busy_worker worker }
11482
end
11583

116-
# @api private
84+
# @!visibility private
11785
def ready_worker(worker)
11886
synchronize { ns_ready_worker worker }
11987
end
12088

121-
# @api private
89+
# @!visibility private
12290
def worker_not_old_enough(worker)
12391
synchronize { ns_worker_not_old_enough worker }
12492
end
12593

126-
# @api private
94+
# @!visibility private
12795
def worker_died(worker)
12896
synchronize { ns_worker_died worker }
12997
end
@@ -303,6 +271,7 @@ def ns_prune_pool
303271
@next_gc_time = Concurrent.monotonic_time + @gc_interval
304272
end
305273

274+
# @!visibility private
306275
class Worker
307276
include Concern::Logging
308277

0 commit comments

Comments
 (0)