Skip to content

Commit aafa28e

Browse files
committed
Better documentation of Executors.
1 parent f79b28b commit aafa28e

8 files changed

+199
-131
lines changed

lib/concurrent/executor/cached_thread_pool.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Concurrent
1515
private_constant :CachedThreadPoolImplementation
1616

1717
# @!macro [attach] cached_thread_pool
18+
#
1819
# A thread pool that dynamically grows and shrinks to fit the current workload.
1920
# New threads are created as needed, existing threads are reused, and threads
2021
# that remain idle for too long are killed and removed from the pool. These
@@ -35,11 +36,7 @@ module Concurrent
3536
#
3637
# The API and behavior of this class are based on Java's `CachedThreadPool`
3738
#
38-
# @see Concurrent::RubyCachedThreadPool
39-
# @see Concurrent::JavaCachedThreadPool
40-
#
4139
# @!macro thread_pool_options
42-
#
4340
# @!macro thread_pool_executor_public_api
4441
class CachedThreadPool < CachedThreadPoolImplementation
4542

lib/concurrent/executor/executor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Concurrent
55

6-
# @api private
6+
# @!visibility private
77
module Executor
88
extend Concern::Deprecation
99

lib/concurrent/executor/executor_service.rb

Lines changed: 172 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,88 @@
77

88
module Concurrent
99

10-
# @api private
10+
# @!macro [new] executor_service_method_post
11+
#
12+
# Submit a task to the executor for asynchronous processing.
13+
#
14+
# @param [Array] args zero or more arguments to be passed to the task
15+
#
16+
# @yield the asynchronous task to perform
17+
#
18+
# @return [Boolean] `true` if the task is queued, `false` if the executor
19+
# is not running
20+
#
21+
# @raise [ArgumentError] if no task is given
22+
23+
# @!macro [new] executor_service_method_left_shift
24+
#
25+
# Submit a task to the executor for asynchronous processing.
26+
#
27+
# @param [Proc] task the asynchronous task to perform
28+
#
29+
# @return [self] returns itself
30+
31+
# @!macro [new] executor_service_method_can_overflow_question
32+
#
33+
# Does the task queue have a maximum size?
34+
#
35+
# @return [Boolean] True if the task queue has a maximum size else false.
36+
37+
# @!macro [new] executor_service_method_serialized_question
38+
#
39+
# Does this executor guarantee serialization of its operations?
40+
#
41+
# @return [Boolean] True if the executor guarantees that all operations
42+
# will be post in the order they are received and no two operations may
43+
# occur simultaneously. Else false.
44+
45+
46+
47+
48+
49+
# @!macro [new] executor_service_public_api
50+
#
51+
# @!method post(*args, &task)
52+
# @!macro executor_service_method_post
53+
#
54+
# @!method <<(task)
55+
# @!macro executor_service_method_left_shift
56+
#
57+
# @!method can_overflow?
58+
# @!macro executor_service_method_can_overflow_question
59+
#
60+
# @!method serialized?
61+
# @!macro executor_service_method_serialized_question
62+
63+
64+
65+
66+
67+
# @!macro executor_service_public_api
68+
# @!visibility private
1169
module ExecutorService
1270
include Concern::Logging
1371
include Concern::Deprecation
1472

15-
# @!macro [attach] executor_service_method_post
16-
#
17-
# Submit a task to the executor for asynchronous processing.
18-
#
19-
# @param [Array] args zero or more arguments to be passed to the task
20-
#
21-
# @yield the asynchronous task to perform
22-
#
23-
# @return [Boolean] `true` if the task is queued, `false` if the executor
24-
# is not running
25-
#
26-
# @raise [ArgumentError] if no task is given
73+
# @!macro executor_service_method_post
2774
def post(*args, &task)
2875
raise NotImplementedError
2976
end
3077

31-
# @!macro [attach] executor_service_method_left_shift
32-
#
33-
# Submit a task to the executor for asynchronous processing.
34-
#
35-
# @param [Proc] task the asynchronous task to perform
36-
#
37-
# @return [self] returns itself
78+
# @!macro executor_service_method_left_shift
3879
def <<(task)
3980
post(&task)
4081
self
4182
end
4283

43-
# @!macro [attach] executor_service_method_can_overflow_question
44-
#
45-
# Does the task queue have a maximum size?
46-
#
47-
# @return [Boolean] True if the task queue has a maximum size else false.
84+
# @!macro executor_service_method_can_overflow_question
4885
#
4986
# @note Always returns `false`
5087
def can_overflow?
5188
false
5289
end
5390

54-
# @!macro [attach] executor_service_method_serialized_question
55-
#
56-
# Does this executor guarantee serialization of its operations?
57-
#
58-
# @return [Boolean] True if the executor guarantees that all operations
59-
# will be post in the order they are received and no two operations may
60-
# occur simultaneously. Else false.
91+
# @!macro executor_service_method_serialized_question
6192
#
6293
# @note Always returns `false`
6394
def serialized?
@@ -83,7 +114,7 @@ def serialized?
83114
# foo.is_a? Concurrent::SerialExecutor #=> true
84115
# foo.serialized? #=> true
85116
#
86-
# @api private
117+
# @!visibility private
87118
module SerialExecutorService
88119
include ExecutorService
89120

@@ -95,6 +126,110 @@ def serialized?
95126
end
96127
end
97128

129+
130+
131+
132+
133+
# @!macro [new] executor_service_attr_reader_fallback_policy
134+
# @return [Symbol] The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.
135+
136+
# @!macro [new] executor_service_method_shutdown
137+
#
138+
# Begin an orderly shutdown. Tasks already in the queue will be executed,
139+
# but no new tasks will be accepted. Has no additional effect if the
140+
# thread pool is not running.
141+
142+
# @!macro [new] executor_service_method_kill
143+
#
144+
# Begin an immediate shutdown. In-progress tasks will be allowed to
145+
# complete but enqueued tasks will be dismissed and no new tasks
146+
# will be accepted. Has no additional effect if the thread pool is
147+
# not running.
148+
149+
# @!macro [new] executor_service_method_wait_for_termination
150+
#
151+
# Block until executor shutdown is complete or until `timeout` seconds have
152+
# passed.
153+
#
154+
# @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
155+
# must be called before this method (or on another thread).
156+
#
157+
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
158+
#
159+
# @return [Boolean] `true` if shutdown complete or false on `timeout`
160+
161+
# @!macro [new] executor_service_method_running_question
162+
#
163+
# Is the executor running?
164+
#
165+
# @return [Boolean] `true` when running, `false` when shutting down or shutdown
166+
167+
# @!macro [new] executor_service_method_shuttingdown_question
168+
#
169+
# Is the executor shuttingdown?
170+
#
171+
# @return [Boolean] `true` when not running and not shutdown, else `false`
172+
173+
# @!macro [new] executor_service_method_shutdown_question
174+
#
175+
# Is the executor shutdown?
176+
#
177+
# @return [Boolean] `true` when shutdown, `false` when shutting down or running
178+
179+
# @!macro [new] executor_service_method_auto_terminate_question
180+
#
181+
# Is the executor auto-terminate when the application exits?
182+
#
183+
# @return [Boolean] `true` when auto-termination is enabled else `false`.
184+
185+
# @!macro [new] 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`.
192+
193+
194+
195+
196+
197+
# @!macro [new] abstract_executor_service_public_api
198+
#
199+
# @!macro executor_service_public_api
200+
#
201+
# @!attribute [r] fallback_policy
202+
# @!macro executor_service_attr_reader_fallback_policy
203+
#
204+
# @!method shutdown
205+
# @!macro executor_service_method_shutdown
206+
#
207+
# @!method kill
208+
# @!macro executor_service_method_kill
209+
#
210+
# @!method wait_for_termination(timeout = nil)
211+
# @!macro executor_service_method_wait_for_termination
212+
#
213+
# @!method running?
214+
# @!macro executor_service_method_running_question
215+
#
216+
# @!method shuttingdown?
217+
# @!macro executor_service_method_shuttingdown_question
218+
#
219+
# @!method shutdown?
220+
# @!macro executor_service_method_shutdown_question
221+
#
222+
# @!method auto_terminate?
223+
# @!macro executor_service_method_auto_terminate_question
224+
#
225+
# @!method auto_terminate=(value)
226+
# @!macro executor_service_method_auto_terminate_setter
227+
228+
229+
230+
231+
232+
# @!macro abstract_executor_service_public_api
98233
# @!visibility private
99234
class AbstractExecutorService < Synchronization::Object
100235
include ExecutorService
@@ -103,7 +238,6 @@ class AbstractExecutorService < Synchronization::Object
103238
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
104239

105240
# @!macro [attach] executor_service_attr_reader_fallback_policy
106-
# @return [Symbol] The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.
107241
attr_reader :fallback_policy
108242

109243
# Create a new thread pool.
@@ -113,82 +247,41 @@ def initialize(*args, &block)
113247
end
114248

115249
# @!macro [attach] executor_service_method_shutdown
116-
#
117-
# Begin an orderly shutdown. Tasks already in the queue will be executed,
118-
# but no new tasks will be accepted. Has no additional effect if the
119-
# thread pool is not running.
120250
def shutdown
121251
raise NotImplementedError
122252
end
123253

124254
# @!macro [attach] executor_service_method_kill
125-
#
126-
# Begin an immediate shutdown. In-progress tasks will be allowed to
127-
# complete but enqueued tasks will be dismissed and no new tasks
128-
# will be accepted. Has no additional effect if the thread pool is
129-
# not running.
130255
def kill
131256
raise NotImplementedError
132257
end
133258

134259
# @!macro [attach] executor_service_method_wait_for_termination
135-
#
136-
# Block until executor shutdown is complete or until `timeout` seconds have
137-
# passed.
138-
#
139-
# @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
140-
# must be called before this method (or on another thread).
141-
#
142-
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
143-
#
144-
# @return [Boolean] `true` if shutdown complete or false on `timeout`
145260
def wait_for_termination(timeout = nil)
146261
raise NotImplementedError
147262
end
148263

149264
# @!macro [attach] executor_service_method_running_question
150-
#
151-
# Is the executor running?
152-
#
153-
# @return [Boolean] `true` when running, `false` when shutting down or shutdown
154265
def running?
155266
synchronize { ns_running? }
156267
end
157268

158269
# @!macro [attach] executor_service_method_shuttingdown_question
159-
#
160-
# Is the executor shuttingdown?
161-
#
162-
# @return [Boolean] `true` when not running and not shutdown, else `false`
163270
def shuttingdown?
164271
synchronize { ns_shuttingdown? }
165272
end
166273

167274
# @!macro [attach] executor_service_method_shutdown_question
168-
#
169-
# Is the executor shutdown?
170-
#
171-
# @return [Boolean] `true` when shutdown, `false` when shutting down or running
172275
def shutdown?
173276
synchronize { ns_shutdown? }
174277
end
175278

176279
# @!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`.
181280
def auto_terminate?
182281
synchronize { ns_auto_terminate? }
183282
end
184283

185284
# @!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`.
192285
def auto_terminate=(value)
193286
synchronize { self.ns_auto_terminate = value }
194287
end
@@ -265,7 +358,8 @@ def terminate_at_exit
265358
end
266359
end
267360

268-
# @api private
361+
# @!macro abstract_executor_service_public_api
362+
# @!visibility private
269363
class RubyExecutorService < AbstractExecutorService
270364

271365
def initialize(*args, &block)
@@ -333,7 +427,8 @@ def ns_shutdown?
333427

334428
if Concurrent.on_jruby?
335429

336-
# @api private
430+
# @!macro abstract_executor_service_public_api
431+
# @!visibility private
337432
class JavaExecutorService < AbstractExecutorService
338433
java_import 'java.lang.Runnable'
339434

0 commit comments

Comments
 (0)