concurrent-ruby 1.1.5

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.
Files changed (143) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +478 -0
  3. data/Gemfile +41 -0
  4. data/LICENSE.md +23 -0
  5. data/README.md +381 -0
  6. data/Rakefile +327 -0
  7. data/ext/concurrent-ruby/ConcurrentRubyService.java +17 -0
  8. data/ext/concurrent-ruby/com/concurrent_ruby/ext/AtomicReferenceLibrary.java +175 -0
  9. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java +248 -0
  10. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicBooleanLibrary.java +93 -0
  11. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +113 -0
  12. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +159 -0
  13. data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +307 -0
  14. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMap.java +31 -0
  15. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java +3863 -0
  16. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/LongAdder.java +203 -0
  17. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/Striped64.java +342 -0
  18. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java +3800 -0
  19. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/LongAdder.java +204 -0
  20. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/Striped64.java +291 -0
  21. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166y/ThreadLocalRandom.java +199 -0
  22. data/lib/concurrent-ruby.rb +1 -0
  23. data/lib/concurrent.rb +134 -0
  24. data/lib/concurrent/agent.rb +587 -0
  25. data/lib/concurrent/array.rb +66 -0
  26. data/lib/concurrent/async.rb +459 -0
  27. data/lib/concurrent/atom.rb +222 -0
  28. data/lib/concurrent/atomic/abstract_thread_local_var.rb +66 -0
  29. data/lib/concurrent/atomic/atomic_boolean.rb +126 -0
  30. data/lib/concurrent/atomic/atomic_fixnum.rb +143 -0
  31. data/lib/concurrent/atomic/atomic_markable_reference.rb +164 -0
  32. data/lib/concurrent/atomic/atomic_reference.rb +204 -0
  33. data/lib/concurrent/atomic/count_down_latch.rb +100 -0
  34. data/lib/concurrent/atomic/cyclic_barrier.rb +128 -0
  35. data/lib/concurrent/atomic/event.rb +109 -0
  36. data/lib/concurrent/atomic/java_count_down_latch.rb +42 -0
  37. data/lib/concurrent/atomic/java_thread_local_var.rb +37 -0
  38. data/lib/concurrent/atomic/mutex_atomic_boolean.rb +62 -0
  39. data/lib/concurrent/atomic/mutex_atomic_fixnum.rb +75 -0
  40. data/lib/concurrent/atomic/mutex_count_down_latch.rb +44 -0
  41. data/lib/concurrent/atomic/mutex_semaphore.rb +115 -0
  42. data/lib/concurrent/atomic/read_write_lock.rb +254 -0
  43. data/lib/concurrent/atomic/reentrant_read_write_lock.rb +379 -0
  44. data/lib/concurrent/atomic/ruby_thread_local_var.rb +161 -0
  45. data/lib/concurrent/atomic/semaphore.rb +145 -0
  46. data/lib/concurrent/atomic/thread_local_var.rb +104 -0
  47. data/lib/concurrent/atomic_reference/mutex_atomic.rb +56 -0
  48. data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +28 -0
  49. data/lib/concurrent/atomics.rb +10 -0
  50. data/lib/concurrent/collection/copy_on_notify_observer_set.rb +107 -0
  51. data/lib/concurrent/collection/copy_on_write_observer_set.rb +111 -0
  52. data/lib/concurrent/collection/java_non_concurrent_priority_queue.rb +84 -0
  53. data/lib/concurrent/collection/lock_free_stack.rb +158 -0
  54. data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +927 -0
  55. data/lib/concurrent/collection/map/mri_map_backend.rb +66 -0
  56. data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +140 -0
  57. data/lib/concurrent/collection/map/synchronized_map_backend.rb +82 -0
  58. data/lib/concurrent/collection/non_concurrent_priority_queue.rb +143 -0
  59. data/lib/concurrent/collection/ruby_non_concurrent_priority_queue.rb +150 -0
  60. data/lib/concurrent/concern/deprecation.rb +34 -0
  61. data/lib/concurrent/concern/dereferenceable.rb +73 -0
  62. data/lib/concurrent/concern/logging.rb +32 -0
  63. data/lib/concurrent/concern/obligation.rb +220 -0
  64. data/lib/concurrent/concern/observable.rb +110 -0
  65. data/lib/concurrent/concurrent_ruby.jar +0 -0
  66. data/lib/concurrent/configuration.rb +184 -0
  67. data/lib/concurrent/constants.rb +8 -0
  68. data/lib/concurrent/dataflow.rb +81 -0
  69. data/lib/concurrent/delay.rb +199 -0
  70. data/lib/concurrent/errors.rb +69 -0
  71. data/lib/concurrent/exchanger.rb +352 -0
  72. data/lib/concurrent/executor/abstract_executor_service.rb +134 -0
  73. data/lib/concurrent/executor/cached_thread_pool.rb +62 -0
  74. data/lib/concurrent/executor/executor_service.rb +185 -0
  75. data/lib/concurrent/executor/fixed_thread_pool.rb +206 -0
  76. data/lib/concurrent/executor/immediate_executor.rb +66 -0
  77. data/lib/concurrent/executor/indirect_immediate_executor.rb +44 -0
  78. data/lib/concurrent/executor/java_executor_service.rb +91 -0
  79. data/lib/concurrent/executor/java_single_thread_executor.rb +29 -0
  80. data/lib/concurrent/executor/java_thread_pool_executor.rb +123 -0
  81. data/lib/concurrent/executor/ruby_executor_service.rb +78 -0
  82. data/lib/concurrent/executor/ruby_single_thread_executor.rb +22 -0
  83. data/lib/concurrent/executor/ruby_thread_pool_executor.rb +362 -0
  84. data/lib/concurrent/executor/safe_task_executor.rb +35 -0
  85. data/lib/concurrent/executor/serial_executor_service.rb +34 -0
  86. data/lib/concurrent/executor/serialized_execution.rb +107 -0
  87. data/lib/concurrent/executor/serialized_execution_delegator.rb +28 -0
  88. data/lib/concurrent/executor/simple_executor_service.rb +100 -0
  89. data/lib/concurrent/executor/single_thread_executor.rb +56 -0
  90. data/lib/concurrent/executor/thread_pool_executor.rb +87 -0
  91. data/lib/concurrent/executor/timer_set.rb +173 -0
  92. data/lib/concurrent/executors.rb +20 -0
  93. data/lib/concurrent/future.rb +141 -0
  94. data/lib/concurrent/hash.rb +59 -0
  95. data/lib/concurrent/immutable_struct.rb +93 -0
  96. data/lib/concurrent/ivar.rb +207 -0
  97. data/lib/concurrent/map.rb +337 -0
  98. data/lib/concurrent/maybe.rb +229 -0
  99. data/lib/concurrent/mutable_struct.rb +229 -0
  100. data/lib/concurrent/mvar.rb +242 -0
  101. data/lib/concurrent/options.rb +42 -0
  102. data/lib/concurrent/promise.rb +579 -0
  103. data/lib/concurrent/promises.rb +2167 -0
  104. data/lib/concurrent/re_include.rb +58 -0
  105. data/lib/concurrent/scheduled_task.rb +318 -0
  106. data/lib/concurrent/set.rb +66 -0
  107. data/lib/concurrent/settable_struct.rb +129 -0
  108. data/lib/concurrent/synchronization.rb +30 -0
  109. data/lib/concurrent/synchronization/abstract_lockable_object.rb +98 -0
  110. data/lib/concurrent/synchronization/abstract_object.rb +24 -0
  111. data/lib/concurrent/synchronization/abstract_struct.rb +160 -0
  112. data/lib/concurrent/synchronization/condition.rb +60 -0
  113. data/lib/concurrent/synchronization/jruby_lockable_object.rb +13 -0
  114. data/lib/concurrent/synchronization/jruby_object.rb +45 -0
  115. data/lib/concurrent/synchronization/lock.rb +36 -0
  116. data/lib/concurrent/synchronization/lockable_object.rb +74 -0
  117. data/lib/concurrent/synchronization/mri_object.rb +44 -0
  118. data/lib/concurrent/synchronization/mutex_lockable_object.rb +76 -0
  119. data/lib/concurrent/synchronization/object.rb +183 -0
  120. data/lib/concurrent/synchronization/rbx_lockable_object.rb +65 -0
  121. data/lib/concurrent/synchronization/rbx_object.rb +49 -0
  122. data/lib/concurrent/synchronization/truffleruby_object.rb +47 -0
  123. data/lib/concurrent/synchronization/volatile.rb +36 -0
  124. data/lib/concurrent/thread_safe/synchronized_delegator.rb +50 -0
  125. data/lib/concurrent/thread_safe/util.rb +16 -0
  126. data/lib/concurrent/thread_safe/util/adder.rb +74 -0
  127. data/lib/concurrent/thread_safe/util/cheap_lockable.rb +118 -0
  128. data/lib/concurrent/thread_safe/util/data_structures.rb +63 -0
  129. data/lib/concurrent/thread_safe/util/power_of_two_tuple.rb +38 -0
  130. data/lib/concurrent/thread_safe/util/striped64.rb +246 -0
  131. data/lib/concurrent/thread_safe/util/volatile.rb +75 -0
  132. data/lib/concurrent/thread_safe/util/xor_shift_random.rb +50 -0
  133. data/lib/concurrent/timer_task.rb +334 -0
  134. data/lib/concurrent/tuple.rb +86 -0
  135. data/lib/concurrent/tvar.rb +258 -0
  136. data/lib/concurrent/utility/at_exit.rb +97 -0
  137. data/lib/concurrent/utility/engine.rb +56 -0
  138. data/lib/concurrent/utility/monotonic_time.rb +58 -0
  139. data/lib/concurrent/utility/native_extension_loader.rb +79 -0
  140. data/lib/concurrent/utility/native_integer.rb +53 -0
  141. data/lib/concurrent/utility/processor_counter.rb +158 -0
  142. data/lib/concurrent/version.rb +3 -0
  143. metadata +193 -0
@@ -0,0 +1,62 @@
1
+ require 'concurrent/utility/engine'
2
+ require 'concurrent/executor/thread_pool_executor'
3
+
4
+ module Concurrent
5
+
6
+ # A thread pool that dynamically grows and shrinks to fit the current workload.
7
+ # New threads are created as needed, existing threads are reused, and threads
8
+ # that remain idle for too long are killed and removed from the pool. These
9
+ # pools are particularly suited to applications that perform a high volume of
10
+ # short-lived tasks.
11
+ #
12
+ # On creation a `CachedThreadPool` has zero running threads. New threads are
13
+ # created on the pool as new operations are `#post`. The size of the pool
14
+ # will grow until `#max_length` threads are in the pool or until the number
15
+ # of threads exceeds the number of running and pending operations. When a new
16
+ # operation is post to the pool the first available idle thread will be tasked
17
+ # with the new operation.
18
+ #
19
+ # Should a thread crash for any reason the thread will immediately be removed
20
+ # from the pool. Similarly, threads which remain idle for an extended period
21
+ # of time will be killed and reclaimed. Thus these thread pools are very
22
+ # efficient at reclaiming unused resources.
23
+ #
24
+ # The API and behavior of this class are based on Java's `CachedThreadPool`
25
+ #
26
+ # @!macro thread_pool_options
27
+ class CachedThreadPool < ThreadPoolExecutor
28
+
29
+ # @!macro cached_thread_pool_method_initialize
30
+ #
31
+ # Create a new thread pool.
32
+ #
33
+ # @param [Hash] opts the options defining pool behavior.
34
+ # @option opts [Symbol] :fallback_policy (`:abort`) the fallback policy
35
+ #
36
+ # @raise [ArgumentError] if `fallback_policy` is not a known policy
37
+ #
38
+ # @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--
39
+ def initialize(opts = {})
40
+ defaults = { idletime: DEFAULT_THREAD_IDLETIMEOUT }
41
+ overrides = { min_threads: 0,
42
+ max_threads: DEFAULT_MAX_POOL_SIZE,
43
+ max_queue: DEFAULT_MAX_QUEUE_SIZE }
44
+ super(defaults.merge(opts).merge(overrides))
45
+ end
46
+
47
+ private
48
+
49
+ # @!macro cached_thread_pool_method_initialize
50
+ # @!visibility private
51
+ def ns_initialize(opts)
52
+ super(opts)
53
+ if Concurrent.on_jruby?
54
+ @max_queue = 0
55
+ @executor = java.util.concurrent.Executors.newCachedThreadPool
56
+ @executor.setRejectedExecutionHandler(FALLBACK_POLICY_CLASSES[@fallback_policy].new)
57
+ @executor.setKeepAliveTime(opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT), java.util.concurrent.TimeUnit::SECONDS)
58
+ self.auto_terminate = opts.fetch(:auto_terminate, true)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,185 @@
1
+ require 'concurrent/concern/logging'
2
+
3
+ module Concurrent
4
+
5
+ ###################################################################
6
+
7
+ # @!macro executor_service_method_post
8
+ #
9
+ # Submit a task to the executor for asynchronous processing.
10
+ #
11
+ # @param [Array] args zero or more arguments to be passed to the task
12
+ #
13
+ # @yield the asynchronous task to perform
14
+ #
15
+ # @return [Boolean] `true` if the task is queued, `false` if the executor
16
+ # is not running
17
+ #
18
+ # @raise [ArgumentError] if no task is given
19
+
20
+ # @!macro executor_service_method_left_shift
21
+ #
22
+ # Submit a task to the executor for asynchronous processing.
23
+ #
24
+ # @param [Proc] task the asynchronous task to perform
25
+ #
26
+ # @return [self] returns itself
27
+
28
+ # @!macro executor_service_method_can_overflow_question
29
+ #
30
+ # Does the task queue have a maximum size?
31
+ #
32
+ # @return [Boolean] True if the task queue has a maximum size else false.
33
+
34
+ # @!macro executor_service_method_serialized_question
35
+ #
36
+ # Does this executor guarantee serialization of its operations?
37
+ #
38
+ # @return [Boolean] True if the executor guarantees that all operations
39
+ # will be post in the order they are received and no two operations may
40
+ # occur simultaneously. Else false.
41
+
42
+ ###################################################################
43
+
44
+ # @!macro executor_service_public_api
45
+ #
46
+ # @!method post(*args, &task)
47
+ # @!macro executor_service_method_post
48
+ #
49
+ # @!method <<(task)
50
+ # @!macro executor_service_method_left_shift
51
+ #
52
+ # @!method can_overflow?
53
+ # @!macro executor_service_method_can_overflow_question
54
+ #
55
+ # @!method serialized?
56
+ # @!macro executor_service_method_serialized_question
57
+
58
+ ###################################################################
59
+
60
+ # @!macro executor_service_attr_reader_fallback_policy
61
+ # @return [Symbol] The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.
62
+
63
+ # @!macro executor_service_method_shutdown
64
+ #
65
+ # Begin an orderly shutdown. Tasks already in the queue will be executed,
66
+ # but no new tasks will be accepted. Has no additional effect if the
67
+ # thread pool is not running.
68
+
69
+ # @!macro executor_service_method_kill
70
+ #
71
+ # Begin an immediate shutdown. In-progress tasks will be allowed to
72
+ # complete but enqueued tasks will be dismissed and no new tasks
73
+ # will be accepted. Has no additional effect if the thread pool is
74
+ # not running.
75
+
76
+ # @!macro executor_service_method_wait_for_termination
77
+ #
78
+ # Block until executor shutdown is complete or until `timeout` seconds have
79
+ # passed.
80
+ #
81
+ # @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
82
+ # must be called before this method (or on another thread).
83
+ #
84
+ # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
85
+ #
86
+ # @return [Boolean] `true` if shutdown complete or false on `timeout`
87
+
88
+ # @!macro executor_service_method_running_question
89
+ #
90
+ # Is the executor running?
91
+ #
92
+ # @return [Boolean] `true` when running, `false` when shutting down or shutdown
93
+
94
+ # @!macro executor_service_method_shuttingdown_question
95
+ #
96
+ # Is the executor shuttingdown?
97
+ #
98
+ # @return [Boolean] `true` when not running and not shutdown, else `false`
99
+
100
+ # @!macro executor_service_method_shutdown_question
101
+ #
102
+ # Is the executor shutdown?
103
+ #
104
+ # @return [Boolean] `true` when shutdown, `false` when shutting down or running
105
+
106
+ # @!macro executor_service_method_auto_terminate_question
107
+ #
108
+ # Is the executor auto-terminate when the application exits?
109
+ #
110
+ # @return [Boolean] `true` when auto-termination is enabled else `false`.
111
+
112
+ # @!macro executor_service_method_auto_terminate_setter
113
+ #
114
+ # Set the auto-terminate behavior for this executor.
115
+ #
116
+ # @param [Boolean] value The new auto-terminate value to set for this executor.
117
+ #
118
+ # @return [Boolean] `true` when auto-termination is enabled else `false`.
119
+
120
+ ###################################################################
121
+
122
+ # @!macro abstract_executor_service_public_api
123
+ #
124
+ # @!macro executor_service_public_api
125
+ #
126
+ # @!attribute [r] fallback_policy
127
+ # @!macro executor_service_attr_reader_fallback_policy
128
+ #
129
+ # @!method shutdown
130
+ # @!macro executor_service_method_shutdown
131
+ #
132
+ # @!method kill
133
+ # @!macro executor_service_method_kill
134
+ #
135
+ # @!method wait_for_termination(timeout = nil)
136
+ # @!macro executor_service_method_wait_for_termination
137
+ #
138
+ # @!method running?
139
+ # @!macro executor_service_method_running_question
140
+ #
141
+ # @!method shuttingdown?
142
+ # @!macro executor_service_method_shuttingdown_question
143
+ #
144
+ # @!method shutdown?
145
+ # @!macro executor_service_method_shutdown_question
146
+ #
147
+ # @!method auto_terminate?
148
+ # @!macro executor_service_method_auto_terminate_question
149
+ #
150
+ # @!method auto_terminate=(value)
151
+ # @!macro executor_service_method_auto_terminate_setter
152
+
153
+ ###################################################################
154
+
155
+ # @!macro executor_service_public_api
156
+ # @!visibility private
157
+ module ExecutorService
158
+ include Concern::Logging
159
+
160
+ # @!macro executor_service_method_post
161
+ def post(*args, &task)
162
+ raise NotImplementedError
163
+ end
164
+
165
+ # @!macro executor_service_method_left_shift
166
+ def <<(task)
167
+ post(&task)
168
+ self
169
+ end
170
+
171
+ # @!macro executor_service_method_can_overflow_question
172
+ #
173
+ # @note Always returns `false`
174
+ def can_overflow?
175
+ false
176
+ end
177
+
178
+ # @!macro executor_service_method_serialized_question
179
+ #
180
+ # @note Always returns `false`
181
+ def serialized?
182
+ false
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,206 @@
1
+ require 'concurrent/utility/engine'
2
+ require 'concurrent/executor/thread_pool_executor'
3
+
4
+ module Concurrent
5
+
6
+ # @!macro thread_pool_executor_constant_default_max_pool_size
7
+ # Default maximum number of threads that will be created in the pool.
8
+
9
+ # @!macro thread_pool_executor_constant_default_min_pool_size
10
+ # Default minimum number of threads that will be retained in the pool.
11
+
12
+ # @!macro 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 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 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 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 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 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 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 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 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 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 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 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 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 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` and no new threads can be created,
120
+ # subsequent tasks will be 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 exits. 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 fixed_thread_pool
173
+ #
174
+ # A thread pool that reuses a fixed number of threads operating off an unbounded queue.
175
+ # At any point, at most `num_threads` will be active processing tasks. 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 fixed_thread_pool_method_initialize
186
+ #
187
+ # Create a new thread pool.
188
+ #
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
192
+ #
193
+ # @raise [ArgumentError] if `num_threads` is less than or equal to zero
194
+ # @raise [ArgumentError] if `fallback_policy` is not a known policy
195
+ #
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))
204
+ end
205
+ end
206
+ end