concurrent-ruby 1.1.5 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +65 -1
- data/Gemfile +6 -10
- data/{LICENSE.md → LICENSE.txt} +18 -20
- data/README.md +46 -23
- data/Rakefile +46 -44
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +0 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +52 -22
- data/lib/{concurrent → concurrent-ruby/concurrent}/agent.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/array.rb +6 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/async.rb +10 -20
- data/lib/{concurrent → concurrent-ruby/concurrent}/atom.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/abstract_thread_local_var.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_boolean.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_fixnum.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_markable_reference.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_reference.rb +1 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/count_down_latch.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/cyclic_barrier.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/event.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_count_down_latch.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_thread_local_var.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_boolean.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_fixnum.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_count_down_latch.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_semaphore.rb +18 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/read_write_lock.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/reentrant_read_write_lock.rb +4 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/ruby_thread_local_var.rb +60 -40
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/semaphore.rb +26 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/thread_local_var.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/mutex_atomic.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/numeric_cas_wrapper.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/atomics.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_notify_observer_set.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_write_observer_set.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/java_non_concurrent_priority_queue.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/lock_free_stack.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/atomic_reference_map_backend.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/mri_map_backend.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/non_concurrent_map_backend.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/synchronized_map_backend.rb +0 -0
- data/lib/concurrent-ruby/concurrent/collection/map/truffleruby_map_backend.rb +14 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/non_concurrent_priority_queue.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/collection/ruby_non_concurrent_priority_queue.rb +11 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/deprecation.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/dereferenceable.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/logging.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/obligation.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/concern/observable.rb +0 -0
- data/lib/concurrent-ruby/concurrent/concurrent_ruby.jar +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/configuration.rb +13 -9
- data/lib/{concurrent → concurrent-ruby/concurrent}/constants.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/dataflow.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/delay.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/errors.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/exchanger.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/abstract_executor_service.rb +33 -36
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/cached_thread_pool.rb +4 -4
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/executor_service.rb +2 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/fixed_thread_pool.rb +29 -15
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/immediate_executor.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/indirect_immediate_executor.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_executor_service.rb +19 -7
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_single_thread_executor.rb +4 -3
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_thread_pool_executor.rb +19 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_executor_service.rb +10 -6
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_single_thread_executor.rb +0 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_thread_pool_executor.rb +46 -42
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/safe_task_executor.rb +5 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serial_executor_service.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution_delegator.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/simple_executor_service.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/single_thread_executor.rb +1 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/thread_pool_executor.rb +2 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executor/timer_set.rb +0 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/executors.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/future.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/hash.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/immutable_struct.rb +9 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/ivar.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/map.rb +14 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/maybe.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/mutable_struct.rb +12 -2
- data/lib/{concurrent → concurrent-ruby/concurrent}/mvar.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/options.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/promise.rb +1 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/promises.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/re_include.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/scheduled_task.rb +29 -16
- data/lib/{concurrent → concurrent-ruby/concurrent}/set.rb +19 -11
- data/lib/{concurrent → concurrent-ruby/concurrent}/settable_struct.rb +11 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_lockable_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_struct.rb +11 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/condition.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_lockable_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lock.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lockable_object.rb +3 -5
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/mri_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/mutex_lockable_object.rb +12 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/rbx_lockable_object.rb +6 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/rbx_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/truffleruby_object.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/volatile.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/synchronized_delegator.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/adder.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/cheap_lockable.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/data_structures.rb +26 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/power_of_two_tuple.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/striped64.rb +1 -1
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/volatile.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/xor_shift_random.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/timer_task.rb +11 -34
- data/lib/{concurrent → concurrent-ruby/concurrent}/tuple.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/tvar.rb +19 -56
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/engine.rb +0 -0
- data/lib/concurrent-ruby/concurrent/utility/monotonic_time.rb +90 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_extension_loader.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_integer.rb +0 -0
- data/lib/{concurrent → concurrent-ruby/concurrent}/utility/processor_counter.rb +5 -33
- data/lib/concurrent-ruby/concurrent/version.rb +3 -0
- data/lib/concurrent-ruby/concurrent-ruby.rb +5 -0
- data/lib/{concurrent.rb → concurrent-ruby/concurrent.rb} +0 -0
- metadata +132 -132
- data/lib/concurrent/concurrent_ruby.jar +0 -0
- data/lib/concurrent/utility/at_exit.rb +0 -97
- data/lib/concurrent/utility/monotonic_time.rb +0 -58
- data/lib/concurrent/version.rb +0 -3
- data/lib/concurrent-ruby.rb +0 -1
File without changes
|
File without changes
|
File without changes
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'concurrent/errors'
|
2
|
+
require 'concurrent/concern/deprecation'
|
2
3
|
require 'concurrent/executor/executor_service'
|
3
4
|
require 'concurrent/synchronization'
|
4
|
-
require 'concurrent/utility/at_exit'
|
5
5
|
|
6
6
|
module Concurrent
|
7
7
|
|
@@ -9,6 +9,7 @@ module Concurrent
|
|
9
9
|
# @!visibility private
|
10
10
|
class AbstractExecutorService < Synchronization::LockableObject
|
11
11
|
include ExecutorService
|
12
|
+
include Concern::Deprecation
|
12
13
|
|
13
14
|
# The set of possible fallback policies that may be set at thread pool creation.
|
14
15
|
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
|
@@ -16,10 +17,20 @@ module Concurrent
|
|
16
17
|
# @!macro executor_service_attr_reader_fallback_policy
|
17
18
|
attr_reader :fallback_policy
|
18
19
|
|
20
|
+
attr_reader :name
|
21
|
+
|
19
22
|
# Create a new thread pool.
|
20
|
-
def initialize(
|
23
|
+
def initialize(opts = {}, &block)
|
21
24
|
super(&nil)
|
22
|
-
synchronize
|
25
|
+
synchronize do
|
26
|
+
@auto_terminate = opts.fetch(:auto_terminate, true)
|
27
|
+
@name = opts.fetch(:name) if opts.key?(:name)
|
28
|
+
ns_initialize(opts, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
name ? "#{super[0..-2]} name: #{name}>" : super
|
23
34
|
end
|
24
35
|
|
25
36
|
# @!macro executor_service_method_shutdown
|
@@ -54,38 +65,41 @@ module Concurrent
|
|
54
65
|
|
55
66
|
# @!macro executor_service_method_auto_terminate_question
|
56
67
|
def auto_terminate?
|
57
|
-
synchronize {
|
68
|
+
synchronize { @auto_terminate }
|
58
69
|
end
|
59
70
|
|
60
71
|
# @!macro executor_service_method_auto_terminate_setter
|
61
72
|
def auto_terminate=(value)
|
62
|
-
|
73
|
+
deprecated "Method #auto_terminate= has no effect. Set :auto_terminate option when executor is initialized."
|
63
74
|
end
|
64
75
|
|
65
76
|
private
|
66
77
|
|
67
|
-
#
|
68
|
-
# reaches `max_queue`.
|
78
|
+
# Returns an action which executes the `fallback_policy` once the queue
|
79
|
+
# size reaches `max_queue`. The reason for the indirection of an action
|
80
|
+
# is so that the work can be deferred outside of synchronization.
|
69
81
|
#
|
70
82
|
# @param [Array] args the arguments to the task which is being handled.
|
71
83
|
#
|
72
84
|
# @!visibility private
|
73
|
-
def
|
85
|
+
def fallback_action(*args)
|
74
86
|
case fallback_policy
|
75
87
|
when :abort
|
76
|
-
raise RejectedExecutionError
|
88
|
+
lambda { raise RejectedExecutionError }
|
77
89
|
when :discard
|
78
|
-
false
|
90
|
+
lambda { false }
|
79
91
|
when :caller_runs
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
lambda {
|
93
|
+
begin
|
94
|
+
yield(*args)
|
95
|
+
rescue => ex
|
96
|
+
# let it fail
|
97
|
+
log DEBUG, ex
|
98
|
+
end
|
99
|
+
true
|
100
|
+
}
|
87
101
|
else
|
88
|
-
fail "Unknown fallback policy #{fallback_policy}"
|
102
|
+
lambda { fail "Unknown fallback policy #{fallback_policy}" }
|
89
103
|
end
|
90
104
|
end
|
91
105
|
|
@@ -110,25 +124,8 @@ module Concurrent
|
|
110
124
|
end
|
111
125
|
|
112
126
|
def ns_auto_terminate?
|
113
|
-
|
127
|
+
@auto_terminate
|
114
128
|
end
|
115
129
|
|
116
|
-
def ns_auto_terminate=(value)
|
117
|
-
case value
|
118
|
-
when true
|
119
|
-
AtExit.add(self) { terminate_at_exit }
|
120
|
-
@auto_terminate = true
|
121
|
-
when false
|
122
|
-
AtExit.delete(self)
|
123
|
-
@auto_terminate = false
|
124
|
-
else
|
125
|
-
raise ArgumentError
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def terminate_at_exit
|
130
|
-
kill # TODO be gentle first
|
131
|
-
wait_for_termination(10)
|
132
|
-
end
|
133
130
|
end
|
134
131
|
end
|
@@ -37,7 +37,7 @@ module Concurrent
|
|
37
37
|
#
|
38
38
|
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
|
39
39
|
def initialize(opts = {})
|
40
|
-
defaults = { idletime:
|
40
|
+
defaults = { idletime: DEFAULT_THREAD_IDLETIMEOUT }
|
41
41
|
overrides = { min_threads: 0,
|
42
42
|
max_threads: DEFAULT_MAX_POOL_SIZE,
|
43
43
|
max_queue: DEFAULT_MAX_QUEUE_SIZE }
|
@@ -51,11 +51,11 @@ module Concurrent
|
|
51
51
|
def ns_initialize(opts)
|
52
52
|
super(opts)
|
53
53
|
if Concurrent.on_jruby?
|
54
|
-
@max_queue
|
55
|
-
@executor
|
54
|
+
@max_queue = 0
|
55
|
+
@executor = java.util.concurrent.Executors.newCachedThreadPool(
|
56
|
+
DaemonThreadFactory.new(ns_auto_terminate?))
|
56
57
|
@executor.setRejectedExecutionHandler(FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
57
58
|
@executor.setKeepAliveTime(opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT), java.util.concurrent.TimeUnit::SECONDS)
|
58
|
-
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -111,10 +111,10 @@ module Concurrent
|
|
111
111
|
|
112
112
|
# @!macro executor_service_method_auto_terminate_setter
|
113
113
|
#
|
114
|
-
# Set the auto-terminate behavior for this executor.
|
115
114
|
#
|
115
|
+
# Set the auto-terminate behavior for this executor.
|
116
|
+
# @deprecated Has no effect
|
116
117
|
# @param [Boolean] value The new auto-terminate value to set for this executor.
|
117
|
-
#
|
118
118
|
# @return [Boolean] `true` when auto-termination is enabled else `false`.
|
119
119
|
|
120
120
|
###################################################################
|
@@ -16,6 +16,9 @@ module Concurrent
|
|
16
16
|
# Default maximum number of seconds a thread in the pool may remain idle
|
17
17
|
# before being reclaimed.
|
18
18
|
|
19
|
+
# @!macro thread_pool_executor_constant_default_synchronous
|
20
|
+
# Default value of the :synchronous option.
|
21
|
+
|
19
22
|
# @!macro thread_pool_executor_attr_reader_max_length
|
20
23
|
# The maximum number of threads that may be created in the pool.
|
21
24
|
# @return [Integer] The maximum number of threads that may be created in the pool.
|
@@ -40,6 +43,10 @@ module Concurrent
|
|
40
43
|
# The number of seconds that a thread may be idle before being reclaimed.
|
41
44
|
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
|
42
45
|
|
46
|
+
# @!macro thread_pool_executor_attr_reader_synchronous
|
47
|
+
# Whether or not a value of 0 for :max_queue option means the queue must perform direct hand-off or rather unbounded queue.
|
48
|
+
# @return [true, false]
|
49
|
+
|
43
50
|
# @!macro thread_pool_executor_attr_reader_max_queue
|
44
51
|
# The maximum number of tasks that may be waiting in the work queue at any one time.
|
45
52
|
# When the queue size reaches `max_queue` subsequent tasks will be rejected in
|
@@ -64,9 +71,16 @@ module Concurrent
|
|
64
71
|
# @return [Integer] Number of tasks that may be enqueued before reaching `max_queue` and rejecting
|
65
72
|
# new tasks. A value of -1 indicates that the queue may grow without bound.
|
66
73
|
|
67
|
-
|
68
|
-
|
69
|
-
|
74
|
+
# @!macro thread_pool_executor_method_prune_pool
|
75
|
+
# Prune the thread pool of unneeded threads
|
76
|
+
#
|
77
|
+
# What is being pruned is controlled by the min_threads and idletime
|
78
|
+
# parameters passed at pool creation time
|
79
|
+
#
|
80
|
+
# This is a no-op on some pool implementation (e.g. the Java one). The Ruby
|
81
|
+
# pool will auto-prune each time a new job is posted. You will need to call
|
82
|
+
# this method explicitely in case your application post jobs in bursts (a
|
83
|
+
# lot of jobs and then nothing for long periods)
|
70
84
|
|
71
85
|
# @!macro thread_pool_executor_public_api
|
72
86
|
#
|
@@ -104,6 +118,9 @@ module Concurrent
|
|
104
118
|
#
|
105
119
|
# @!method can_overflow?
|
106
120
|
# @!macro executor_service_method_can_overflow_question
|
121
|
+
#
|
122
|
+
# @!method prune_pool
|
123
|
+
# @!macro thread_pool_executor_method_prune_pool
|
107
124
|
|
108
125
|
|
109
126
|
|
@@ -115,12 +132,13 @@ module Concurrent
|
|
115
132
|
# Thread pools support several configuration options:
|
116
133
|
#
|
117
134
|
# * `idletime`: The number of seconds that a thread may be idle before being reclaimed.
|
135
|
+
# * `name`: The name of the executor (optional). Printed in the executor's `#to_s` output and
|
136
|
+
# a `<name>-worker-<id>` name is given to its threads if supported by used Ruby
|
137
|
+
# implementation. `<id>` is uniq for each thread.
|
118
138
|
# * `max_queue`: The maximum number of tasks that may be waiting in the work queue at
|
119
139
|
# any one time. When the queue size reaches `max_queue` and no new threads can be created,
|
120
140
|
# subsequent tasks will be rejected in accordance with the configured `fallback_policy`.
|
121
|
-
# * `auto_terminate`: When true (default)
|
122
|
-
# will stop the thread pool when the application exits. See below for more information
|
123
|
-
# on shutting down thread pools.
|
141
|
+
# * `auto_terminate`: When true (default), the threads started will be marked as daemon.
|
124
142
|
# * `fallback_policy`: The policy defining how rejected tasks are handled.
|
125
143
|
#
|
126
144
|
# Three fallback policies are supported:
|
@@ -145,16 +163,12 @@ module Concurrent
|
|
145
163
|
#
|
146
164
|
# On some runtime platforms (most notably the JVM) the application will not
|
147
165
|
# exit until all thread pools have been shutdown. To prevent applications from
|
148
|
-
# "hanging" on exit all
|
149
|
-
#
|
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.
|
166
|
+
# "hanging" on exit, all threads can be marked as daemon according to the
|
167
|
+
# `:auto_terminate` option.
|
154
168
|
#
|
155
169
|
# ```ruby
|
156
|
-
# pool1 = Concurrent::FixedThreadPool.new(5) #
|
157
|
-
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) #
|
170
|
+
# pool1 = Concurrent::FixedThreadPool.new(5) # threads will be marked as daemon
|
171
|
+
# pool2 = Concurrent::FixedThreadPool.new(5, auto_terminate: false) # mark threads as non-daemon
|
158
172
|
# ```
|
159
173
|
#
|
160
174
|
# @note Failure to properly shutdown a thread pool can lead to unpredictable results.
|
@@ -163,7 +177,7 @@ module Concurrent
|
|
163
177
|
# @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Java Tutorials: Thread Pools
|
164
178
|
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html Java Executors class
|
165
179
|
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html Java ExecutorService interface
|
166
|
-
# @see
|
180
|
+
# @see https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setDaemon-boolean-
|
167
181
|
|
168
182
|
|
169
183
|
|
File without changes
|
File without changes
|
@@ -18,13 +18,9 @@ if Concurrent.on_jruby?
|
|
18
18
|
}.freeze
|
19
19
|
private_constant :FALLBACK_POLICY_CLASSES
|
20
20
|
|
21
|
-
def initialize(*args, &block)
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
21
|
def post(*args, &task)
|
26
22
|
raise ArgumentError.new('no block given') unless block_given?
|
27
|
-
return
|
23
|
+
return fallback_action(*args, &task).call unless running?
|
28
24
|
@executor.submit Job.new(args, task)
|
29
25
|
true
|
30
26
|
rescue Java::JavaUtilConcurrent::RejectedExecutionException
|
@@ -42,7 +38,6 @@ if Concurrent.on_jruby?
|
|
42
38
|
|
43
39
|
def shutdown
|
44
40
|
synchronize do
|
45
|
-
self.ns_auto_terminate = false
|
46
41
|
@executor.shutdown
|
47
42
|
nil
|
48
43
|
end
|
@@ -50,7 +45,6 @@ if Concurrent.on_jruby?
|
|
50
45
|
|
51
46
|
def kill
|
52
47
|
synchronize do
|
53
|
-
self.ns_auto_terminate = false
|
54
48
|
@executor.shutdownNow
|
55
49
|
nil
|
56
50
|
end
|
@@ -87,5 +81,23 @@ if Concurrent.on_jruby?
|
|
87
81
|
end
|
88
82
|
private_constant :Job
|
89
83
|
end
|
84
|
+
|
85
|
+
class DaemonThreadFactory
|
86
|
+
# hide include from YARD
|
87
|
+
send :include, java.util.concurrent.ThreadFactory
|
88
|
+
|
89
|
+
def initialize(daemonize = true)
|
90
|
+
@daemonize = daemonize
|
91
|
+
end
|
92
|
+
|
93
|
+
def newThread(runnable)
|
94
|
+
thread = java.util.concurrent.Executors.defaultThreadFactory().newThread(runnable)
|
95
|
+
thread.setDaemon(@daemonize)
|
96
|
+
return thread
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private_constant :DaemonThreadFactory
|
101
|
+
|
90
102
|
end
|
91
103
|
end
|
@@ -17,12 +17,13 @@ if Concurrent.on_jruby?
|
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
-
|
20
|
+
|
21
21
|
def ns_initialize(opts)
|
22
|
-
@executor = java.util.concurrent.Executors.newSingleThreadExecutor
|
22
|
+
@executor = java.util.concurrent.Executors.newSingleThreadExecutor(
|
23
|
+
DaemonThreadFactory.new(ns_auto_terminate?)
|
24
|
+
)
|
23
25
|
@fallback_policy = opts.fetch(:fallback_policy, :discard)
|
24
26
|
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)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
@@ -21,12 +21,18 @@ if Concurrent.on_jruby?
|
|
21
21
|
# @!macro thread_pool_executor_constant_default_thread_timeout
|
22
22
|
DEFAULT_THREAD_IDLETIMEOUT = 60
|
23
23
|
|
24
|
+
# @!macro thread_pool_executor_constant_default_synchronous
|
25
|
+
DEFAULT_SYNCHRONOUS = false
|
26
|
+
|
24
27
|
# @!macro thread_pool_executor_attr_reader_max_length
|
25
28
|
attr_reader :max_length
|
26
29
|
|
27
30
|
# @!macro thread_pool_executor_attr_reader_max_queue
|
28
31
|
attr_reader :max_queue
|
29
32
|
|
33
|
+
# @!macro thread_pool_executor_attr_reader_synchronous
|
34
|
+
attr_reader :synchronous
|
35
|
+
|
30
36
|
# @!macro thread_pool_executor_method_initialize
|
31
37
|
def initialize(opts = {})
|
32
38
|
super(opts)
|
@@ -87,6 +93,10 @@ if Concurrent.on_jruby?
|
|
87
93
|
super && !@executor.isTerminating
|
88
94
|
end
|
89
95
|
|
96
|
+
# @!macro thread_pool_executor_method_prune_pool
|
97
|
+
def prune_pool
|
98
|
+
end
|
99
|
+
|
90
100
|
private
|
91
101
|
|
92
102
|
def ns_initialize(opts)
|
@@ -94,8 +104,10 @@ if Concurrent.on_jruby?
|
|
94
104
|
max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
|
95
105
|
idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
|
96
106
|
@max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
|
107
|
+
@synchronous = opts.fetch(:synchronous, DEFAULT_SYNCHRONOUS)
|
97
108
|
@fallback_policy = opts.fetch(:fallback_policy, :abort)
|
98
109
|
|
110
|
+
raise ArgumentError.new("`synchronous` cannot be set unless `max_queue` is 0") if @synchronous && @max_queue > 0
|
99
111
|
raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if max_length < DEFAULT_MIN_POOL_SIZE
|
100
112
|
raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if max_length > DEFAULT_MAX_POOL_SIZE
|
101
113
|
raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if min_length < DEFAULT_MIN_POOL_SIZE
|
@@ -103,7 +115,11 @@ if Concurrent.on_jruby?
|
|
103
115
|
raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.include?(@fallback_policy)
|
104
116
|
|
105
117
|
if @max_queue == 0
|
106
|
-
|
118
|
+
if @synchronous
|
119
|
+
queue = java.util.concurrent.SynchronousQueue.new
|
120
|
+
else
|
121
|
+
queue = java.util.concurrent.LinkedBlockingQueue.new
|
122
|
+
end
|
107
123
|
else
|
108
124
|
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
|
109
125
|
end
|
@@ -114,10 +130,11 @@ if Concurrent.on_jruby?
|
|
114
130
|
idletime,
|
115
131
|
java.util.concurrent.TimeUnit::SECONDS,
|
116
132
|
queue,
|
133
|
+
DaemonThreadFactory.new(ns_auto_terminate?),
|
117
134
|
FALLBACK_POLICY_CLASSES[@fallback_policy].new)
|
118
135
|
|
119
|
-
self.auto_terminate = opts.fetch(:auto_terminate, true)
|
120
136
|
end
|
121
137
|
end
|
138
|
+
|
122
139
|
end
|
123
140
|
end
|
@@ -16,10 +16,16 @@ module Concurrent
|
|
16
16
|
|
17
17
|
def post(*args, &task)
|
18
18
|
raise ArgumentError.new('no block given') unless block_given?
|
19
|
-
synchronize
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
deferred_action = synchronize {
|
20
|
+
if running?
|
21
|
+
ns_execute(*args, &task)
|
22
|
+
else
|
23
|
+
fallback_action(*args, &task)
|
24
|
+
end
|
25
|
+
}
|
26
|
+
if deferred_action
|
27
|
+
deferred_action.call
|
28
|
+
else
|
23
29
|
true
|
24
30
|
end
|
25
31
|
end
|
@@ -27,7 +33,6 @@ module Concurrent
|
|
27
33
|
def shutdown
|
28
34
|
synchronize do
|
29
35
|
break unless running?
|
30
|
-
self.ns_auto_terminate = false
|
31
36
|
stop_event.set
|
32
37
|
ns_shutdown_execution
|
33
38
|
end
|
@@ -37,7 +42,6 @@ module Concurrent
|
|
37
42
|
def kill
|
38
43
|
synchronize do
|
39
44
|
break if shutdown?
|
40
|
-
self.ns_auto_terminate = false
|
41
45
|
stop_event.set
|
42
46
|
ns_kill_execution
|
43
47
|
stopped_event.set
|