@@ -14,16 +14,18 @@ module RubyExecutor
14
14
include Executor
15
15
include Logging
16
16
17
- # Submit a task to the executor for asynchronous processing.
17
+ # @!macro [attach] executor_method_post
18
18
#
19
- # @param [Array] args zero or more arguments to be passed to the task
19
+ # Submit a task to the executor for asynchronous processing.
20
20
#
21
- # @yield the asynchronous task to perform
21
+ # @param [Array] args zero or more arguments to be passed to the task
22
22
#
23
- # @return [Boolean] `true` if the task is queued, `false` if the executor
24
- # is not running
23
+ # @yield the asynchronous task to perform
25
24
#
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
27
29
def post ( *args , &task )
28
30
raise ArgumentError . new ( 'no block given' ) unless block_given?
29
31
mutex . synchronize do
@@ -33,40 +35,50 @@ def post(*args, &task)
33
35
end
34
36
end
35
37
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.
37
41
#
38
- # @param [Proc] task the asynchronous task to perform
42
+ # @param [Proc] task the asynchronous task to perform
39
43
#
40
- # @return [self] returns itself
44
+ # @return [self] returns itself
41
45
def <<( task )
42
46
post ( &task )
43
47
self
44
48
end
45
49
46
- # Is the executor running?
50
+ # @!macro [attach] executor_method_running_question
51
+ #
52
+ # Is the executor running?
47
53
#
48
- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
54
+ # @return [Boolean] `true` when running, `false` when shutting down or shutdown
49
55
def running?
50
56
! stop_event . set?
51
57
end
52
58
53
- # Is the executor shuttingdown?
59
+ # @!macro [attach] executor_method_shuttingdown_question
54
60
#
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`
56
64
def shuttingdown?
57
65
! ( running? || shutdown? )
58
66
end
59
67
60
- # Is the executor shutdown?
68
+ # @!macro [attach] executor_method_shutdown_question
69
+ #
70
+ # Is the executor shutdown?
61
71
#
62
- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
72
+ # @return [Boolean] `true` when shutdown, `false` when shutting down or running
63
73
def shutdown?
64
74
stopped_event . set?
65
75
end
66
76
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.
70
82
def shutdown
71
83
mutex . synchronize do
72
84
break unless running?
@@ -76,10 +88,12 @@ def shutdown
76
88
true
77
89
end
78
90
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.
83
97
def kill
84
98
mutex . synchronize do
85
99
break if shutdown?
@@ -90,15 +104,17 @@ def kill
90
104
true
91
105
end
92
106
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.
95
111
#
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).
98
114
#
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
100
116
#
101
- # @return [Boolean] `true` if shutdown complete or false on `timeout`
117
+ # @return [Boolean] `true` if shutdown complete or false on `timeout`
102
118
def wait_for_termination ( timeout = nil )
103
119
stopped_event . wait ( timeout )
104
120
end
@@ -107,20 +123,33 @@ def wait_for_termination(timeout = nil)
107
123
108
124
attr_reader :mutex , :stop_event , :stopped_event
109
125
126
+ # @!macro [attach] executor_method_init_executor
127
+ #
128
+ # Initialize the executor by creating and initializing all the
129
+ # internal synchronization objects.
110
130
def init_executor
111
131
@mutex = Mutex . new
112
132
@stop_event = Event . new
113
133
@stopped_event = Event . new
114
134
end
115
135
136
+ # @!macro [attach] executor_method_execute
116
137
def execute ( *args , &task )
117
138
raise NotImplementedError
118
139
end
119
140
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.
120
145
def shutdown_execution
121
146
stopped_event . set
122
147
end
123
148
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.
124
153
def kill_execution
125
154
# do nothing
126
155
end
@@ -131,16 +160,7 @@ def kill_execution
131
160
module JavaExecutor
132
161
include Executor
133
162
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
144
164
def post ( *args )
145
165
raise ArgumentError . new ( 'no block given' ) unless block_given?
146
166
if running?
@@ -153,26 +173,18 @@ def post(*args)
153
173
raise RejectedExecutionError
154
174
end
155
175
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
161
177
def <<( task )
162
178
post ( &task )
163
179
self
164
180
end
165
181
166
- # Is the executor running?
167
- #
168
- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
182
+ # @!macro executor_method_running_question
169
183
def running?
170
184
! ( shuttingdown? || shutdown? )
171
185
end
172
186
173
- # Is the executor shuttingdown?
174
- #
175
- # @return [Boolean] `true` when not running and not shutdown, else `false`
187
+ # @!macro executor_method_shuttingdown_question
176
188
def shuttingdown?
177
189
if @executor . respond_to? :isTerminating
178
190
@executor . isTerminating
@@ -181,38 +193,23 @@ def shuttingdown?
181
193
end
182
194
end
183
195
184
- # Is the executor shutdown?
185
- #
186
- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
196
+ # @!macro executor_method_shutdown_question
187
197
def shutdown?
188
198
@executor . isShutdown || @executor . isTerminated
189
199
end
190
200
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
200
202
def wait_for_termination ( timeout )
201
203
@executor . awaitTermination ( 1000 * timeout , java . util . concurrent . TimeUnit ::MILLISECONDS )
202
204
end
203
205
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
207
207
def shutdown
208
208
@executor . shutdown
209
209
nil
210
210
end
211
211
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
216
213
def kill
217
214
@executor . shutdownNow
218
215
nil
0 commit comments