concurrent-ruby 0.8.0.pre2-java → 0.9.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -3
- data/README.md +111 -55
- data/lib/concurrent.rb +90 -14
- data/lib/concurrent/async.rb +143 -51
- data/lib/concurrent/atom.rb +131 -0
- data/lib/concurrent/atomic/atomic_boolean.rb +57 -107
- data/lib/concurrent/atomic/atomic_fixnum.rb +73 -101
- data/lib/concurrent/atomic/atomic_reference.rb +49 -0
- data/lib/concurrent/atomic/condition.rb +23 -12
- data/lib/concurrent/atomic/count_down_latch.rb +23 -21
- data/lib/concurrent/atomic/cyclic_barrier.rb +47 -47
- data/lib/concurrent/atomic/event.rb +33 -42
- data/lib/concurrent/atomic/read_write_lock.rb +252 -0
- data/lib/concurrent/atomic/semaphore.rb +64 -89
- data/lib/concurrent/atomic/thread_local_var.rb +130 -58
- data/lib/concurrent/atomic/thread_local_var/weak_key_map.rb +236 -0
- data/lib/concurrent/atomic_reference/direct_update.rb +34 -3
- data/lib/concurrent/atomic_reference/jruby.rb +6 -3
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +17 -39
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +3 -0
- data/lib/concurrent/atomic_reference/rbx.rb +4 -1
- data/lib/concurrent/atomic_reference/ruby.rb +6 -3
- data/lib/concurrent/atomics.rb +74 -4
- data/lib/concurrent/collection/copy_on_notify_observer_set.rb +115 -0
- data/lib/concurrent/collection/copy_on_write_observer_set.rb +119 -0
- data/lib/concurrent/collection/priority_queue.rb +300 -245
- data/lib/concurrent/concern/deprecation.rb +34 -0
- data/lib/concurrent/concern/dereferenceable.rb +88 -0
- data/lib/concurrent/concern/logging.rb +27 -0
- data/lib/concurrent/concern/obligation.rb +228 -0
- data/lib/concurrent/concern/observable.rb +85 -0
- data/lib/concurrent/configuration.rb +234 -109
- data/lib/concurrent/dataflow.rb +2 -3
- data/lib/concurrent/delay.rb +141 -50
- data/lib/concurrent/edge.rb +30 -0
- data/lib/concurrent/errors.rb +19 -7
- data/lib/concurrent/exchanger.rb +25 -1
- data/lib/concurrent/executor/cached_thread_pool.rb +51 -33
- data/lib/concurrent/executor/executor.rb +46 -299
- data/lib/concurrent/executor/executor_service.rb +521 -0
- data/lib/concurrent/executor/fixed_thread_pool.rb +196 -23
- data/lib/concurrent/executor/immediate_executor.rb +9 -9
- data/lib/concurrent/executor/indirect_immediate_executor.rb +4 -3
- data/lib/concurrent/executor/java_single_thread_executor.rb +17 -16
- data/lib/concurrent/executor/java_thread_pool_executor.rb +55 -102
- data/lib/concurrent/executor/ruby_single_thread_executor.rb +14 -16
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +250 -166
- data/lib/concurrent/executor/safe_task_executor.rb +5 -4
- data/lib/concurrent/executor/serialized_execution.rb +22 -18
- data/lib/concurrent/executor/{per_thread_executor.rb → simple_executor_service.rb} +29 -20
- data/lib/concurrent/executor/single_thread_executor.rb +32 -21
- data/lib/concurrent/executor/thread_pool_executor.rb +73 -60
- data/lib/concurrent/executor/timer_set.rb +96 -84
- data/lib/concurrent/executors.rb +1 -1
- data/lib/concurrent/future.rb +71 -38
- data/lib/concurrent/immutable_struct.rb +89 -0
- data/lib/concurrent/ivar.rb +152 -60
- data/lib/concurrent/lazy_register.rb +40 -20
- data/lib/concurrent/maybe.rb +226 -0
- data/lib/concurrent/mutable_struct.rb +227 -0
- data/lib/concurrent/mvar.rb +44 -43
- data/lib/concurrent/promise.rb +229 -136
- data/lib/concurrent/scheduled_task.rb +341 -43
- data/lib/concurrent/settable_struct.rb +127 -0
- data/lib/concurrent/synchronization.rb +17 -0
- data/lib/concurrent/synchronization/abstract_object.rb +163 -0
- data/lib/concurrent/synchronization/abstract_struct.rb +158 -0
- data/lib/concurrent/synchronization/condition.rb +53 -0
- data/lib/concurrent/synchronization/java_object.rb +34 -0
- data/lib/concurrent/synchronization/lock.rb +32 -0
- data/lib/concurrent/synchronization/monitor_object.rb +26 -0
- data/lib/concurrent/synchronization/mutex_object.rb +43 -0
- data/lib/concurrent/synchronization/object.rb +78 -0
- data/lib/concurrent/synchronization/rbx_object.rb +75 -0
- data/lib/concurrent/timer_task.rb +92 -103
- data/lib/concurrent/tvar.rb +42 -38
- data/lib/concurrent/utilities.rb +3 -1
- data/lib/concurrent/utility/at_exit.rb +97 -0
- data/lib/concurrent/utility/engine.rb +44 -0
- data/lib/concurrent/utility/monotonic_time.rb +59 -0
- data/lib/concurrent/utility/native_extension_loader.rb +56 -0
- data/lib/concurrent/utility/processor_counter.rb +156 -0
- data/lib/concurrent/utility/timeout.rb +18 -14
- data/lib/concurrent/utility/timer.rb +11 -6
- data/lib/concurrent/version.rb +2 -1
- data/lib/concurrent_ruby.rb +1 -0
- data/lib/concurrent_ruby_ext.jar +0 -0
- metadata +46 -66
- data/lib/concurrent/actor.rb +0 -103
- data/lib/concurrent/actor/behaviour.rb +0 -70
- data/lib/concurrent/actor/behaviour/abstract.rb +0 -48
- data/lib/concurrent/actor/behaviour/awaits.rb +0 -21
- data/lib/concurrent/actor/behaviour/buffer.rb +0 -54
- data/lib/concurrent/actor/behaviour/errors_on_unknown_message.rb +0 -12
- data/lib/concurrent/actor/behaviour/executes_context.rb +0 -18
- data/lib/concurrent/actor/behaviour/linking.rb +0 -45
- data/lib/concurrent/actor/behaviour/pausing.rb +0 -77
- data/lib/concurrent/actor/behaviour/removes_child.rb +0 -16
- data/lib/concurrent/actor/behaviour/sets_results.rb +0 -36
- data/lib/concurrent/actor/behaviour/supervised.rb +0 -59
- data/lib/concurrent/actor/behaviour/supervising.rb +0 -34
- data/lib/concurrent/actor/behaviour/terminates_children.rb +0 -13
- data/lib/concurrent/actor/behaviour/termination.rb +0 -54
- data/lib/concurrent/actor/context.rb +0 -154
- data/lib/concurrent/actor/core.rb +0 -217
- data/lib/concurrent/actor/default_dead_letter_handler.rb +0 -9
- data/lib/concurrent/actor/envelope.rb +0 -41
- data/lib/concurrent/actor/errors.rb +0 -27
- data/lib/concurrent/actor/internal_delegations.rb +0 -49
- data/lib/concurrent/actor/public_delegations.rb +0 -40
- data/lib/concurrent/actor/reference.rb +0 -81
- data/lib/concurrent/actor/root.rb +0 -37
- data/lib/concurrent/actor/type_check.rb +0 -48
- data/lib/concurrent/actor/utils.rb +0 -10
- data/lib/concurrent/actor/utils/ad_hoc.rb +0 -21
- data/lib/concurrent/actor/utils/balancer.rb +0 -42
- data/lib/concurrent/actor/utils/broadcast.rb +0 -52
- data/lib/concurrent/actor/utils/pool.rb +0 -59
- data/lib/concurrent/actress.rb +0 -3
- data/lib/concurrent/agent.rb +0 -209
- data/lib/concurrent/atomic.rb +0 -92
- data/lib/concurrent/atomic/copy_on_notify_observer_set.rb +0 -118
- data/lib/concurrent/atomic/copy_on_write_observer_set.rb +0 -117
- data/lib/concurrent/atomic/synchronization.rb +0 -51
- data/lib/concurrent/channel/buffered_channel.rb +0 -85
- data/lib/concurrent/channel/channel.rb +0 -41
- data/lib/concurrent/channel/unbuffered_channel.rb +0 -35
- data/lib/concurrent/channel/waitable_list.rb +0 -40
- data/lib/concurrent/channels.rb +0 -5
- data/lib/concurrent/collection/blocking_ring_buffer.rb +0 -71
- data/lib/concurrent/collection/ring_buffer.rb +0 -59
- data/lib/concurrent/collections.rb +0 -3
- data/lib/concurrent/dereferenceable.rb +0 -108
- data/lib/concurrent/executor/java_cached_thread_pool.rb +0 -32
- data/lib/concurrent/executor/java_fixed_thread_pool.rb +0 -31
- data/lib/concurrent/executor/ruby_cached_thread_pool.rb +0 -29
- data/lib/concurrent/executor/ruby_fixed_thread_pool.rb +0 -32
- data/lib/concurrent/executor/ruby_thread_pool_worker.rb +0 -73
- data/lib/concurrent/logging.rb +0 -20
- data/lib/concurrent/obligation.rb +0 -171
- data/lib/concurrent/observable.rb +0 -73
- data/lib/concurrent/options_parser.rb +0 -48
- data/lib/concurrent/utility/processor_count.rb +0 -152
- data/lib/extension_helper.rb +0 -37
@@ -1,33 +1,206 @@
|
|
1
|
-
require 'concurrent/
|
1
|
+
require 'concurrent/utility/engine'
|
2
|
+
require 'concurrent/executor/thread_pool_executor'
|
2
3
|
|
3
4
|
module Concurrent
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
# @!macro [new] thread_pool_executor_constant_default_max_pool_size
|
7
|
+
# Default maximum number of threads that will be created in the pool.
|
8
|
+
|
9
|
+
# @!macro [new] thread_pool_executor_constant_default_min_pool_size
|
10
|
+
# Default minimum number of threads that will be retained in the pool.
|
11
|
+
|
12
|
+
# @!macro [new] thread_pool_executor_constant_default_max_queue_size
|
13
|
+
# Default maximum number of tasks that may be added to the task queue.
|
14
|
+
|
15
|
+
# @!macro [new] thread_pool_executor_constant_default_thread_timeout
|
16
|
+
# Default maximum number of seconds a thread in the pool may remain idle
|
17
|
+
# before being reclaimed.
|
18
|
+
|
19
|
+
# @!macro [new] thread_pool_executor_attr_reader_max_length
|
20
|
+
# The maximum number of threads that may be created in the pool.
|
21
|
+
# @return [Integer] The maximum number of threads that may be created in the pool.
|
22
|
+
|
23
|
+
# @!macro [new] thread_pool_executor_attr_reader_min_length
|
24
|
+
# The minimum number of threads that may be retained in the pool.
|
25
|
+
# @return [Integer] The minimum number of threads that may be retained in the pool.
|
26
|
+
|
27
|
+
# @!macro [new] thread_pool_executor_attr_reader_largest_length
|
28
|
+
# The largest number of threads that have been created in the pool since construction.
|
29
|
+
# @return [Integer] The largest number of threads that have been created in the pool since construction.
|
30
|
+
|
31
|
+
# @!macro [new] thread_pool_executor_attr_reader_scheduled_task_count
|
32
|
+
# The number of tasks that have been scheduled for execution on the pool since construction.
|
33
|
+
# @return [Integer] The number of tasks that have been scheduled for execution on the pool since construction.
|
34
|
+
|
35
|
+
# @!macro [new] thread_pool_executor_attr_reader_completed_task_count
|
36
|
+
# The number of tasks that have been completed by the pool since construction.
|
37
|
+
# @return [Integer] The number of tasks that have been completed by the pool since construction.
|
38
|
+
|
39
|
+
# @!macro [new] thread_pool_executor_attr_reader_idletime
|
40
|
+
# The number of seconds that a thread may be idle before being reclaimed.
|
41
|
+
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
|
42
|
+
|
43
|
+
# @!macro [new] thread_pool_executor_attr_reader_max_queue
|
44
|
+
# The maximum number of tasks that may be waiting in the work queue at any one time.
|
45
|
+
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
46
|
+
# accordance with the configured `fallback_policy`.
|
47
|
+
#
|
48
|
+
# @return [Integer] The maximum number of tasks that may be waiting in the work queue at any one time.
|
49
|
+
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
50
|
+
# accordance with the configured `fallback_policy`.
|
51
|
+
|
52
|
+
# @!macro [new] thread_pool_executor_attr_reader_length
|
53
|
+
# The number of threads currently in the pool.
|
54
|
+
# @return [Integer] The number of threads currently in the pool.
|
55
|
+
|
56
|
+
# @!macro [new] thread_pool_executor_attr_reader_queue_length
|
57
|
+
# The number of tasks in the queue awaiting execution.
|
58
|
+
# @return [Integer] The number of tasks in the queue awaiting execution.
|
59
|
+
|
60
|
+
# @!macro [new] thread_pool_executor_attr_reader_remaining_capacity
|
61
|
+
# Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
62
|
+
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
63
|
+
#
|
64
|
+
# @return [Integer] Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
65
|
+
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
# @!macro [new] thread_pool_executor_public_api
|
72
|
+
#
|
73
|
+
# @!macro abstract_executor_service_public_api
|
74
|
+
#
|
75
|
+
# @!attribute [r] max_length
|
76
|
+
# @!macro thread_pool_executor_attr_reader_max_length
|
77
|
+
#
|
78
|
+
# @!attribute [r] min_length
|
79
|
+
# @!macro thread_pool_executor_attr_reader_min_length
|
80
|
+
#
|
81
|
+
# @!attribute [r] largest_length
|
82
|
+
# @!macro thread_pool_executor_attr_reader_largest_length
|
83
|
+
#
|
84
|
+
# @!attribute [r] scheduled_task_count
|
85
|
+
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
86
|
+
#
|
87
|
+
# @!attribute [r] completed_task_count
|
88
|
+
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
89
|
+
#
|
90
|
+
# @!attribute [r] idletime
|
91
|
+
# @!macro thread_pool_executor_attr_reader_idletime
|
92
|
+
#
|
93
|
+
# @!attribute [r] max_queue
|
94
|
+
# @!macro thread_pool_executor_attr_reader_max_queue
|
95
|
+
#
|
96
|
+
# @!attribute [r] length
|
97
|
+
# @!macro thread_pool_executor_attr_reader_length
|
98
|
+
#
|
99
|
+
# @!attribute [r] queue_length
|
100
|
+
# @!macro thread_pool_executor_attr_reader_queue_length
|
101
|
+
#
|
102
|
+
# @!attribute [r] remaining_capacity
|
103
|
+
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
104
|
+
#
|
105
|
+
# @!method can_overflow?
|
106
|
+
# @!macro executor_service_method_can_overflow_question
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# @!macro [new] thread_pool_options
|
112
|
+
#
|
113
|
+
# **Thread Pool Options**
|
114
|
+
#
|
115
|
+
# Thread pools support several configuration options:
|
116
|
+
#
|
117
|
+
# * `idletime`: The number of seconds that a thread may be idle before being reclaimed.
|
118
|
+
# * `max_queue`: The maximum number of tasks that may be waiting in the work queue at
|
119
|
+
# any one time. When the queue size reaches `max_queue` subsequent tasks will be
|
120
|
+
# rejected in accordance with the configured `fallback_policy`.
|
121
|
+
# * `auto_terminate`: When true (default) an `at_exit` handler will be registered which
|
122
|
+
# will stop the thread pool when the application exits. See below for more information
|
123
|
+
# on shutting down thread pools.
|
124
|
+
# * `fallback_policy`: The policy defining how rejected tasks are handled.
|
125
|
+
#
|
126
|
+
# Three fallback policies are supported:
|
127
|
+
#
|
128
|
+
# * `:abort`: Raise a `RejectedExecutionError` exception and discard the task.
|
129
|
+
# * `:discard`: Discard the task and return false.
|
130
|
+
# * `:caller_runs`: Execute the task on the calling thread.
|
131
|
+
#
|
132
|
+
# **Shutting Down Thread Pools**
|
133
|
+
#
|
134
|
+
# Killing a thread pool while tasks are still being processed, either by calling
|
135
|
+
# the `#kill` method or at application exit, will have unpredictable results. There
|
136
|
+
# is no way for the thread pool to know what resources are being used by the
|
137
|
+
# in-progress tasks. When those tasks are killed the impact on those resources
|
138
|
+
# cannot be predicted. The *best* practice is to explicitly shutdown all thread
|
139
|
+
# pools using the provided methods:
|
140
|
+
#
|
141
|
+
# * Call `#shutdown` to initiate an orderly termination of all in-progress tasks
|
142
|
+
# * Call `#wait_for_termination` with an appropriate timeout interval an allow
|
143
|
+
# the orderly shutdown to complete
|
144
|
+
# * Call `#kill` *only when* the thread pool fails to shutdown in the allotted time
|
145
|
+
#
|
146
|
+
# On some runtime platforms (most notably the JVM) the application will not
|
147
|
+
# exit until all thread pools have been shutdown. To prevent applications from
|
148
|
+
# "hanging" on exit all thread pools include an `at_exit` handler that will
|
149
|
+
# stop the thread pool when the application exists. This handler uses a brute
|
150
|
+
# force method to stop the pool and makes no guarantees regarding resources being
|
151
|
+
# used by any tasks still running. Registration of this `at_exit` handler can be
|
152
|
+
# prevented by setting the thread pool's constructor `:auto_terminate` option to
|
153
|
+
# `false` when the thread pool is created. All thread pools support this option.
|
154
|
+
#
|
155
|
+
# ```ruby
|
156
|
+
# pool1 = Concurrent::FixedThreadPool.new(5) # an `at_exit` handler will be registered
|
157
|
+
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) # prevent `at_exit` handler registration
|
158
|
+
# ```
|
159
|
+
#
|
160
|
+
# @note Failure to properly shutdown a thread pool can lead to unpredictable results.
|
161
|
+
# Please read *Shutting Down Thread Pools* for more information.
|
162
|
+
#
|
163
|
+
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Java Tutorials: Thread Pools
|
164
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
|
165
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface
|
166
|
+
# @see http://ruby-doc.org//core-2.2.0/Kernel.html#method-i-at_exit Kernel#at_exit
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
# @!macro [attach] fixed_thread_pool
|
173
|
+
#
|
174
|
+
# A thread pool with a set number of threads. The number of threads in the pool
|
175
|
+
# is set on construction and remains constant. When all threads are busy new
|
176
|
+
# tasks `#post` to the thread pool are enqueued until a thread becomes available.
|
177
|
+
# Should a thread crash for any reason the thread will immediately be removed
|
178
|
+
# from the pool and replaced.
|
179
|
+
#
|
180
|
+
# The API and behavior of this class are based on Java's `FixedThreadPool`
|
181
|
+
#
|
182
|
+
# @!macro thread_pool_options
|
183
|
+
class FixedThreadPool < ThreadPoolExecutor
|
184
|
+
|
185
|
+
# @!macro [attach] fixed_thread_pool_method_initialize
|
14
186
|
#
|
15
|
-
#
|
187
|
+
# Create a new thread pool.
|
16
188
|
#
|
17
|
-
# @
|
18
|
-
#
|
189
|
+
# @param [Integer] num_threads the number of threads to allocate
|
190
|
+
# @param [Hash] opts the options defining pool behavior.
|
191
|
+
# @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy
|
19
192
|
#
|
20
|
-
# @
|
21
|
-
# @
|
193
|
+
# @raise [ArgumentError] if `num_threads` is less than or equal to zero
|
194
|
+
# @raise [ArgumentError] if `fallback_policy` is not a known policy
|
22
195
|
#
|
23
|
-
# @see http://docs.oracle.com/javase/
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
196
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-
|
197
|
+
def initialize(num_threads, opts = {})
|
198
|
+
raise ArgumentError.new('number of threads must be greater than zero') if num_threads.to_i < 1
|
199
|
+
defaults = { max_queue: DEFAULT_MAX_QUEUE_SIZE,
|
200
|
+
idletime: DEFAULT_THREAD_IDLETIMEOUT }
|
201
|
+
overrides = { min_threads: num_threads,
|
202
|
+
max_threads: num_threads }
|
203
|
+
super(defaults.merge(opts).merge(overrides))
|
31
204
|
end
|
32
205
|
end
|
33
206
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'concurrent/atomic/event'
|
2
|
-
require 'concurrent/executor/
|
2
|
+
require 'concurrent/executor/executor_service'
|
3
3
|
|
4
4
|
module Concurrent
|
5
5
|
|
@@ -14,14 +14,14 @@ module Concurrent
|
|
14
14
|
#
|
15
15
|
# @note Intended for use primarily in testing and debugging.
|
16
16
|
class ImmediateExecutor
|
17
|
-
include
|
17
|
+
include SerialExecutorService
|
18
18
|
|
19
19
|
# Creates a new executor
|
20
20
|
def initialize
|
21
21
|
@stopped = Concurrent::Event.new
|
22
22
|
end
|
23
23
|
|
24
|
-
# @!macro
|
24
|
+
# @!macro executor_service_method_post
|
25
25
|
def post(*args, &task)
|
26
26
|
raise ArgumentError.new('no block given') unless block_given?
|
27
27
|
return false unless running?
|
@@ -29,35 +29,35 @@ module Concurrent
|
|
29
29
|
true
|
30
30
|
end
|
31
31
|
|
32
|
-
# @!macro
|
32
|
+
# @!macro executor_service_method_left_shift
|
33
33
|
def <<(task)
|
34
34
|
post(&task)
|
35
35
|
self
|
36
36
|
end
|
37
37
|
|
38
|
-
# @!macro
|
38
|
+
# @!macro executor_service_method_running_question
|
39
39
|
def running?
|
40
40
|
! shutdown?
|
41
41
|
end
|
42
42
|
|
43
|
-
# @!macro
|
43
|
+
# @!macro executor_service_method_shuttingdown_question
|
44
44
|
def shuttingdown?
|
45
45
|
false
|
46
46
|
end
|
47
47
|
|
48
|
-
# @!macro
|
48
|
+
# @!macro executor_service_method_shutdown_question
|
49
49
|
def shutdown?
|
50
50
|
@stopped.set?
|
51
51
|
end
|
52
52
|
|
53
|
-
# @!macro
|
53
|
+
# @!macro executor_service_method_shutdown
|
54
54
|
def shutdown
|
55
55
|
@stopped.set
|
56
56
|
true
|
57
57
|
end
|
58
58
|
alias_method :kill, :shutdown
|
59
59
|
|
60
|
-
# @!macro
|
60
|
+
# @!macro executor_service_method_wait_for_termination
|
61
61
|
def wait_for_termination(timeout = nil)
|
62
62
|
@stopped.wait(timeout)
|
63
63
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'concurrent/executor/
|
1
|
+
require 'concurrent/executor/immediate_executor'
|
2
|
+
require 'concurrent/executor/simple_executor_service'
|
2
3
|
|
3
4
|
module Concurrent
|
4
5
|
# An executor service which runs all operations on a new thread, blocking
|
@@ -19,10 +20,10 @@ module Concurrent
|
|
19
20
|
# Creates a new executor
|
20
21
|
def initialize
|
21
22
|
super
|
22
|
-
@internal_executor =
|
23
|
+
@internal_executor = SimpleExecutorService.new
|
23
24
|
end
|
24
25
|
|
25
|
-
# @!macro
|
26
|
+
# @!macro executor_service_method_post
|
26
27
|
def post(*args, &task)
|
27
28
|
raise ArgumentError.new("no block given") unless block_given?
|
28
29
|
return false unless running?
|
@@ -1,27 +1,28 @@
|
|
1
|
-
if
|
2
|
-
|
1
|
+
if Concurrent.on_jruby?
|
2
|
+
|
3
|
+
require 'concurrent/executor/executor_service'
|
3
4
|
|
4
5
|
module Concurrent
|
5
6
|
|
6
7
|
# @!macro single_thread_executor
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
# @!macro thread_pool_options
|
9
|
+
# @!macro abstract_executor_service_public_api
|
10
|
+
# @!visibility private
|
11
|
+
class JavaSingleThreadExecutor < JavaExecutorService
|
12
|
+
include SerialExecutorService
|
10
13
|
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# @option opts [Symbol] :fallback_policy (:discard) the policy
|
14
|
-
# for handling new tasks that are received when the queue size
|
15
|
-
# has reached `max_queue` or after the executor has shut down
|
16
|
-
#
|
17
|
-
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
|
18
|
-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
|
19
|
-
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
|
14
|
+
# @!macro single_thread_executor_method_initialize
|
20
15
|
def initialize(opts = {})
|
16
|
+
super(opts)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def ns_initialize(opts)
|
21
22
|
@executor = java.util.concurrent.Executors.newSingleThreadExecutor
|
22
23
|
@fallback_policy = opts.fetch(:fallback_policy, :discard)
|
23
|
-
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless
|
24
|
-
|
24
|
+
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.keys.include?(@fallback_policy)
|
25
|
+
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
@@ -1,168 +1,121 @@
|
|
1
|
-
if
|
2
|
-
|
1
|
+
if Concurrent.on_jruby?
|
2
|
+
|
3
|
+
require 'concurrent/executor/executor_service'
|
3
4
|
|
4
5
|
module Concurrent
|
5
6
|
|
6
7
|
# @!macro thread_pool_executor
|
7
|
-
|
8
|
-
|
8
|
+
# @!macro thread_pool_options
|
9
|
+
# @!visibility private
|
10
|
+
class JavaThreadPoolExecutor < JavaExecutorService
|
9
11
|
|
10
|
-
#
|
12
|
+
# @!macro thread_pool_executor_constant_default_max_pool_size
|
11
13
|
DEFAULT_MAX_POOL_SIZE = java.lang.Integer::MAX_VALUE # 2147483647
|
12
14
|
|
13
|
-
#
|
15
|
+
# @!macro thread_pool_executor_constant_default_min_pool_size
|
14
16
|
DEFAULT_MIN_POOL_SIZE = 0
|
15
17
|
|
16
|
-
#
|
18
|
+
# @!macro thread_pool_executor_constant_default_max_queue_size
|
17
19
|
DEFAULT_MAX_QUEUE_SIZE = 0
|
18
20
|
|
19
|
-
#
|
20
|
-
# before being reclaimed.
|
21
|
+
# @!macro thread_pool_executor_constant_default_thread_timeout
|
21
22
|
DEFAULT_THREAD_IDLETIMEOUT = 60
|
22
23
|
|
23
|
-
#
|
24
|
+
# @!macro thread_pool_executor_attr_reader_max_length
|
24
25
|
attr_reader :max_length
|
25
26
|
|
26
|
-
#
|
27
|
-
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
28
|
-
# accordance with the configured `fallback_policy`.
|
27
|
+
# @!macro thread_pool_executor_attr_reader_max_queue
|
29
28
|
attr_reader :max_queue
|
30
29
|
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# @param [Hash] opts the options which configure the thread pool
|
34
|
-
#
|
35
|
-
# @option opts [Integer] :max_threads (DEFAULT_MAX_POOL_SIZE) the maximum
|
36
|
-
# number of threads to be created
|
37
|
-
# @option opts [Integer] :min_threads (DEFAULT_MIN_POOL_SIZE) the minimum
|
38
|
-
# number of threads to be retained
|
39
|
-
# @option opts [Integer] :idletime (DEFAULT_THREAD_IDLETIMEOUT) the maximum
|
40
|
-
# number of seconds a thread may be idle before being reclaimed
|
41
|
-
# @option opts [Integer] :max_queue (DEFAULT_MAX_QUEUE_SIZE) the maximum
|
42
|
-
# number of tasks allowed in the work queue at any one time; a value of
|
43
|
-
# zero means the queue may grow without bound
|
44
|
-
# @option opts [Symbol] :fallback_policy (:abort) the policy for handling new
|
45
|
-
# tasks that are received when the queue size has reached
|
46
|
-
# `max_queue` or the executir has shut down
|
47
|
-
#
|
48
|
-
# @raise [ArgumentError] if `:max_threads` is less than one
|
49
|
-
# @raise [ArgumentError] if `:min_threads` is less than zero
|
50
|
-
# @raise [ArgumentError] if `:fallback_policy` is not one of the values specified
|
51
|
-
# in `FALLBACK_POLICIES`
|
52
|
-
#
|
53
|
-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
|
30
|
+
# @!macro thread_pool_executor_method_initialize
|
54
31
|
def initialize(opts = {})
|
55
|
-
|
56
|
-
max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
57
|
-
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
58
|
-
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
59
|
-
@fallback_policy = opts.fetch(:fallback_policy, opts.fetch(:overflow_policy, :abort))
|
60
|
-
warn '[DEPRECATED] :overflow_policy is deprecated terminology, please use :fallback_policy instead' if opts.has_key?(:overflow_policy)
|
61
|
-
|
62
|
-
raise ArgumentError.new('max_threads must be greater than zero') if max_length <= 0
|
63
|
-
raise ArgumentError.new('min_threads cannot be less than zero') if min_length < 0
|
64
|
-
raise ArgumentError.new('min_threads cannot be more than max_threads') if min_length > max_length
|
65
|
-
raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICIES.include?(@fallback_policy)
|
66
|
-
|
67
|
-
if @max_queue == 0
|
68
|
-
queue = java.util.concurrent.LinkedBlockingQueue.new
|
69
|
-
else
|
70
|
-
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
|
71
|
-
end
|
72
|
-
|
73
|
-
@executor = java.util.concurrent.ThreadPoolExecutor.new(
|
74
|
-
min_length, max_length,
|
75
|
-
idletime, java.util.concurrent.TimeUnit::SECONDS,
|
76
|
-
queue, FALLBACK_POLICIES[@fallback_policy].new)
|
77
|
-
|
78
|
-
set_shutdown_hook
|
32
|
+
super(opts)
|
79
33
|
end
|
80
34
|
|
81
|
-
# @!macro
|
35
|
+
# @!macro executor_service_method_can_overflow_question
|
82
36
|
def can_overflow?
|
83
37
|
@max_queue != 0
|
84
38
|
end
|
85
39
|
|
86
|
-
#
|
87
|
-
#
|
88
|
-
# @return [Integer] the min_length
|
40
|
+
# @!macro thread_pool_executor_attr_reader_min_length
|
89
41
|
def min_length
|
90
42
|
@executor.getCorePoolSize
|
91
43
|
end
|
92
44
|
|
93
|
-
#
|
94
|
-
#
|
95
|
-
# @return [Integer] the max_length
|
45
|
+
# @!macro thread_pool_executor_attr_reader_max_length
|
96
46
|
def max_length
|
97
47
|
@executor.getMaximumPoolSize
|
98
48
|
end
|
99
49
|
|
100
|
-
#
|
101
|
-
#
|
102
|
-
# @return [Integer] the length
|
50
|
+
# @!macro thread_pool_executor_attr_reader_length
|
103
51
|
def length
|
104
52
|
@executor.getPoolSize
|
105
53
|
end
|
106
|
-
alias_method :current_length, :length
|
107
54
|
|
108
|
-
#
|
109
|
-
#
|
110
|
-
# @return [Integer] the largest_length
|
55
|
+
# @!macro thread_pool_executor_attr_reader_largest_length
|
111
56
|
def largest_length
|
112
57
|
@executor.getLargestPoolSize
|
113
58
|
end
|
114
59
|
|
115
|
-
#
|
116
|
-
#
|
117
|
-
# @return [Integer] the scheduled_task_count
|
60
|
+
# @!macro thread_pool_executor_attr_reader_scheduled_task_count
|
118
61
|
def scheduled_task_count
|
119
62
|
@executor.getTaskCount
|
120
63
|
end
|
121
64
|
|
122
|
-
#
|
123
|
-
#
|
124
|
-
# @return [Integer] the completed_task_count
|
65
|
+
# @!macro thread_pool_executor_attr_reader_completed_task_count
|
125
66
|
def completed_task_count
|
126
67
|
@executor.getCompletedTaskCount
|
127
68
|
end
|
128
69
|
|
129
|
-
#
|
130
|
-
#
|
131
|
-
# @return [Integer] the idletime
|
70
|
+
# @!macro thread_pool_executor_attr_reader_idletime
|
132
71
|
def idletime
|
133
72
|
@executor.getKeepAliveTime(java.util.concurrent.TimeUnit::SECONDS)
|
134
73
|
end
|
135
74
|
|
136
|
-
#
|
137
|
-
#
|
138
|
-
# @return [Integer] the queue_length
|
75
|
+
# @!macro thread_pool_executor_attr_reader_queue_length
|
139
76
|
def queue_length
|
140
77
|
@executor.getQueue.size
|
141
78
|
end
|
142
79
|
|
143
|
-
#
|
144
|
-
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
145
|
-
#
|
146
|
-
# @return [Integer] the remaining_capacity
|
80
|
+
# @!macro thread_pool_executor_attr_reader_remaining_capacity
|
147
81
|
def remaining_capacity
|
148
82
|
@max_queue == 0 ? -1 : @executor.getQueue.remainingCapacity
|
149
83
|
end
|
150
84
|
|
151
|
-
#
|
152
|
-
# This method is supost to return the threads status, but Java API doesn't
|
153
|
-
# provide a way to get the thread status. So we return an empty Array instead.
|
154
|
-
def status
|
155
|
-
warn '[DEPRECATED] `status` is deprecated and will be removed soon.'
|
156
|
-
warn "Calls to `status` return an empty Array. Java ThreadPoolExecutor does not provide thread's status."
|
157
|
-
[]
|
158
|
-
end
|
159
|
-
|
160
|
-
# Is the thread pool running?
|
161
|
-
#
|
162
|
-
# @return [Boolean] `true` when running, `false` when shutting down or shutdown
|
85
|
+
# @!macro executor_service_method_running_question
|
163
86
|
def running?
|
164
87
|
super && !@executor.isTerminating
|
165
88
|
end
|
89
|
+
|
90
|
+
protected
|
91
|
+
|
92
|
+
def ns_initialize(opts)
|
93
|
+
min_length = opts.fetch(:min_threads, DEFAULT_MIN_POOL_SIZE).to_i
|
94
|
+
max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
95
|
+
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
96
|
+
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
97
|
+
@fallback_policy = opts.fetch(:fallback_policy, opts.fetch(:overflow_policy, :abort))
|
98
|
+
deprecated ' :overflow_policy is deprecated terminology, please use :fallback_policy instead' if opts.has_key?(:overflow_policy)
|
99
|
+
|
100
|
+
raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if max_length < DEFAULT_MIN_POOL_SIZE
|
101
|
+
raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if max_length > DEFAULT_MAX_POOL_SIZE
|
102
|
+
raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if min_length < DEFAULT_MIN_POOL_SIZE
|
103
|
+
raise ArgumentError.new("`min_threads` cannot be more than `max_threads`") if min_length > max_length
|
104
|
+
raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.include?(@fallback_policy)
|
105
|
+
|
106
|
+
if @max_queue == 0
|
107
|
+
queue = java.util.concurrent.LinkedBlockingQueue.new
|
108
|
+
else
|
109
|
+
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
|
110
|
+
end
|
111
|
+
|
112
|
+
@executor = java.util.concurrent.ThreadPoolExecutor.new(
|
113
|
+
min_length, max_length,
|
114
|
+
idletime, java.util.concurrent.TimeUnit::SECONDS,
|
115
|
+
queue, FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
116
|
+
|
117
|
+
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
118
|
+
end
|
166
119
|
end
|
167
120
|
end
|
168
121
|
end
|