1
- require 'concurrent/executor/ruby_cached_thread_pool'
1
+ require 'concurrent/utility/engine'
2
+ require 'concurrent/executor/thread_pool_executor'
2
3
3
4
module Concurrent
4
5
5
- if Concurrent . on_jruby?
6
- require 'concurrent/executor/java_cached_thread_pool'
7
- end
8
-
9
- CachedThreadPoolImplementation = case
10
- when Concurrent . on_jruby?
11
- JavaCachedThreadPool
12
- else
13
- RubyCachedThreadPool
14
- end
15
- private_constant :CachedThreadPoolImplementation
16
-
17
- # @!macro [attach] cached_thread_pool
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.
18
11
#
19
- # A thread pool that dynamically grows and shrinks to fit the current workload.
20
- # New threads are created as needed, existing threads are reused, and threads
21
- # that remain idle for too long are killed and removed from the pool. These
22
- # pools are particularly suited to applications that perform a high volume of
23
- # short-lived tasks.
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.
24
18
#
25
- # On creation a `CachedThreadPool` has zero running threads. New threads are
26
- # created on the pool as new operations are `#post`. The size of the pool
27
- # will grow until `#max_length` threads are in the pool or until the number
28
- # of threads exceeds the number of running and pending operations. When a new
29
- # operation is post to the pool the first available idle thread will be tasked
30
- # with the new operation.
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.
31
23
#
32
- # Should a thread crash for any reason the thread will immediately be removed
33
- # from the pool. Similarly, threads which remain idle for an extended period
34
- # of time will be killed and reclaimed. Thus these thread pools are very
35
- # efficient at reclaiming unused resources.
36
- #
37
- # The API and behavior of this class are based on Java's `CachedThreadPool`
24
+ # The API and behavior of this class are based on Java's `CachedThreadPool`
38
25
#
39
26
# @!macro thread_pool_options
40
- # @!macro thread_pool_executor_public_api
41
- class CachedThreadPool < CachedThreadPoolImplementation
27
+ class CachedThreadPool < ThreadPoolExecutor
42
28
43
- # @!macro [new ] cached_thread_pool_method_initialize
29
+ # @!macro [attach ] cached_thread_pool_method_initialize
44
30
#
45
31
# Create a new thread pool.
46
32
#
@@ -50,8 +36,27 @@ class CachedThreadPool < CachedThreadPoolImplementation
50
36
# @raise [ArgumentError] if `fallback_policy` is not a known policy
51
37
#
52
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
53
48
54
- # @!method initialize(opts = {})
55
- # @!macro cached_thread_pool_method_initialize
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
56
61
end
57
62
end
0 commit comments