concurrent-ruby 0.8.0 → 0.9.0.pre2

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