@@ -14,16 +14,18 @@ module RubyExecutor
1414 include Executor
1515 include Logging
1616
17- # Submit a task to the executor for asynchronous processing.
17+ # @!macro [attach] executor_method_post
1818 #
19- # @param [Array] args zero or more arguments to be passed to the task
19+ # Submit a task to the executor for asynchronous processing.
2020 #
21- # @yield the asynchronous task to perform
21+ # @param [Array] args zero or more arguments to be passed to the task
2222 #
23- # @return [Boolean] `true` if the task is queued, `false` if the executor
24- # is not running
23+ # @yield the asynchronous task to perform
2524 #
26- # @raise [ArgumentError] if no task is given
25+ # @return [Boolean] `true` if the task is queued, `false` if the executor
26+ # is not running
27+ #
28+ # @raise [ArgumentError] if no task is given
2729 def post ( *args , &task )
2830 raise ArgumentError . new ( 'no block given' ) unless block_given?
2931 mutex . synchronize do
@@ -33,40 +35,50 @@ def post(*args, &task)
3335 end
3436 end
3537
36- # Submit a task to the executor for asynchronous processing.
38+ # @!macro [attach] executor_method_left_shift
39+ #
40+ # Submit a task to the executor for asynchronous processing.
3741 #
38- # @param [Proc] task the asynchronous task to perform
42+ # @param [Proc] task the asynchronous task to perform
3943 #
40- # @return [self] returns itself
44+ # @return [self] returns itself
4145 def <<( task )
4246 post ( &task )
4347 self
4448 end
4549
46- # Is the executor running?
50+ # @!macro [attach] executor_method_running_question
51+ #
52+ # Is the executor running?
4753 #
48- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
54+ # @return [Boolean] `true` when running, `false` when shutting down or shutdown
4955 def running?
5056 ! stop_event . set?
5157 end
5258
53- # Is the executor shuttingdown?
59+ # @!macro [attach] executor_method_shuttingdown_question
5460 #
55- # @return [Boolean] `true` when not running and not shutdown, else `false`
61+ # Is the executor shuttingdown?
62+ #
63+ # @return [Boolean] `true` when not running and not shutdown, else `false`
5664 def shuttingdown?
5765 ! ( running? || shutdown? )
5866 end
5967
60- # Is the executor shutdown?
68+ # @!macro [attach] executor_method_shutdown_question
69+ #
70+ # Is the executor shutdown?
6171 #
62- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
72+ # @return [Boolean] `true` when shutdown, `false` when shutting down or running
6373 def shutdown?
6474 stopped_event . set?
6575 end
6676
67- # Begin an orderly shutdown. Tasks already in the queue will be executed,
68- # but no new tasks will be accepted. Has no additional effect if the
69- # thread pool is not running.
77+ # @!macro [attach] executor_method_shutdown
78+ #
79+ # Begin an orderly shutdown. Tasks already in the queue will be executed,
80+ # but no new tasks will be accepted. Has no additional effect if the
81+ # thread pool is not running.
7082 def shutdown
7183 mutex . synchronize do
7284 break unless running?
@@ -76,10 +88,12 @@ def shutdown
7688 true
7789 end
7890
79- # Begin an immediate shutdown. In-progress tasks will be allowed to
80- # complete but enqueued tasks will be dismissed and no new tasks
81- # will be accepted. Has no additional effect if the thread pool is
82- # not running.
91+ # @!macro [attach] executor_method_kill
92+ #
93+ # Begin an immediate shutdown. In-progress tasks will be allowed to
94+ # complete but enqueued tasks will be dismissed and no new tasks
95+ # will be accepted. Has no additional effect if the thread pool is
96+ # not running.
8397 def kill
8498 mutex . synchronize do
8599 break if shutdown?
@@ -90,15 +104,17 @@ def kill
90104 true
91105 end
92106
93- # Block until executor shutdown is complete or until `timeout` seconds have
94- # passed.
107+ # @!macro [attach] executor_method_wait_for_termination
108+ #
109+ # Block until executor shutdown is complete or until `timeout` seconds have
110+ # passed.
95111 #
96- # @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
97- # must be called before this method (or on another thread).
112+ # @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
113+ # must be called before this method (or on another thread).
98114 #
99- # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
115+ # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
100116 #
101- # @return [Boolean] `true` if shutdown complete or false on `timeout`
117+ # @return [Boolean] `true` if shutdown complete or false on `timeout`
102118 def wait_for_termination ( timeout = nil )
103119 stopped_event . wait ( timeout )
104120 end
@@ -107,20 +123,33 @@ def wait_for_termination(timeout = nil)
107123
108124 attr_reader :mutex , :stop_event , :stopped_event
109125
126+ # @!macro [attach] executor_method_init_executor
127+ #
128+ # Initialize the executor by creating and initializing all the
129+ # internal synchronization objects.
110130 def init_executor
111131 @mutex = Mutex . new
112132 @stop_event = Event . new
113133 @stopped_event = Event . new
114134 end
115135
136+ # @!macro [attach] executor_method_execute
116137 def execute ( *args , &task )
117138 raise NotImplementedError
118139 end
119140
141+ # @!macro [attach] executor_method_shutdown_execution
142+ #
143+ # Callback method called when an orderly shutdown has completed.
144+ # The default behavior is to signal all waiting threads.
120145 def shutdown_execution
121146 stopped_event . set
122147 end
123148
149+ # @!macro [attach] executor_method_kill_execution
150+ #
151+ # Callback method called when the executor has been killed.
152+ # The default behavior is to do nothing.
124153 def kill_execution
125154 # do nothing
126155 end
@@ -131,16 +160,7 @@ def kill_execution
131160 module JavaExecutor
132161 include Executor
133162
134- # Submit a task to the executor for asynchronous processing.
135- #
136- # @param [Array] args zero or more arguments to be passed to the task
137- #
138- # @yield the asynchronous task to perform
139- #
140- # @return [Boolean] `true` if the task is queued, `false` if the executor
141- # is not running
142- #
143- # @raise [ArgumentError] if no task is given
163+ # @!macro executor_method_post
144164 def post ( *args )
145165 raise ArgumentError . new ( 'no block given' ) unless block_given?
146166 if running?
@@ -153,26 +173,18 @@ def post(*args)
153173 raise RejectedExecutionError
154174 end
155175
156- # Submit a task to the executor for asynchronous processing.
157- #
158- # @param [Proc] task the asynchronous task to perform
159- #
160- # @return [self] returns itself
176+ # @!macro executor_method_left_shift
161177 def <<( task )
162178 post ( &task )
163179 self
164180 end
165181
166- # Is the executor running?
167- #
168- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
182+ # @!macro executor_method_running_question
169183 def running?
170184 ! ( shuttingdown? || shutdown? )
171185 end
172186
173- # Is the executor shuttingdown?
174- #
175- # @return [Boolean] `true` when not running and not shutdown, else `false`
187+ # @!macro executor_method_shuttingdown_question
176188 def shuttingdown?
177189 if @executor . respond_to? :isTerminating
178190 @executor . isTerminating
@@ -181,38 +193,23 @@ def shuttingdown?
181193 end
182194 end
183195
184- # Is the executor shutdown?
185- #
186- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
196+ # @!macro executor_method_shutdown_question
187197 def shutdown?
188198 @executor . isShutdown || @executor . isTerminated
189199 end
190200
191- # Block until executor shutdown is complete or until `timeout` seconds have
192- # passed.
193- #
194- # @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
195- # must be called before this method (or on another thread).
196- #
197- # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
198- #
199- # @return [Boolean] `true` if shutdown complete or false on `timeout`
201+ # @!macro executor_method_wait_for_termination
200202 def wait_for_termination ( timeout )
201203 @executor . awaitTermination ( 1000 * timeout , java . util . concurrent . TimeUnit ::MILLISECONDS )
202204 end
203205
204- # Begin an orderly shutdown. Tasks already in the queue will be executed,
205- # but no new tasks will be accepted. Has no additional effect if the
206- # executor is not running.
206+ # @!macro executor_method_shutdown
207207 def shutdown
208208 @executor . shutdown
209209 nil
210210 end
211211
212- # Begin an immediate shutdown. In-progress tasks will be allowed to
213- # complete but enqueued tasks will be dismissed and no new tasks
214- # will be accepted. Has no additional effect if the executor is
215- # not running.
212+ # @!macro executor_method_kill
216213 def kill
217214 @executor . shutdownNow
218215 nil
0 commit comments