Class: Concurrent::JavaCachedThreadPool
- Inherits:
-
JavaThreadPoolExecutor
- Object
- JavaThreadPoolExecutor
- Concurrent::JavaCachedThreadPool
- Defined in:
- lib/concurrent/executor/java_cached_thread_pool.rb
Overview
When running on the JVM (JRuby) this class will inherit from ‘JavaCachedThreadPool`. On all other platforms it will inherit from `RubyCachedThreadPool`.
A thread pool that dynamically grows and shrinks to fit the current workload. New threads are created as needed, existing threads are reused, and threads that remain idle for too long are killed and removed from the pool. These pools are particularly suited to applications that perform a high volume of short-lived tasks.
On creation a ‘CachedThreadPool` has zero running threads. New threads are created on the pool as new operations are `#post`. The size of the pool will grow until `#max_length` threads are in the pool or until the number of threads exceeds the number of running and pending operations. When a new operation is post to the pool the first available idle thread will be tasked with the new operation.
Should a thread crash for any reason the thread will immediately be removed from the pool. Similarly, threads which remain idle for an extended period of time will be killed and reclaimed. Thus these thread pools are very efficient at reclaiming unused resources.
The API and behavior of this class are based on Java’s ‘CachedThreadPool`
Constant Summary
Constants inherited from JavaThreadPoolExecutor
Concurrent::JavaThreadPoolExecutor::DEFAULT_MAX_POOL_SIZE, Concurrent::JavaThreadPoolExecutor::DEFAULT_MAX_QUEUE_SIZE, Concurrent::JavaThreadPoolExecutor::DEFAULT_MIN_POOL_SIZE, Concurrent::JavaThreadPoolExecutor::DEFAULT_THREAD_IDLETIMEOUT, Concurrent::JavaThreadPoolExecutor::OVERFLOW_POLICIES
Instance Attribute Summary
Attributes inherited from JavaThreadPoolExecutor
#max_length, #max_queue, #overflow_policy
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ JavaCachedThreadPool
constructor
Create a new thread pool.
Methods inherited from JavaThreadPoolExecutor
#can_overflow?, #completed_task_count, #idletime, #largest_length, #length, #min_length, #queue_length, #remaining_capacity, #running?, #scheduled_task_count, #status
Methods included from JavaExecutor
#<<, #kill, #post, #running?, #shutdown, #shutdown?, #shuttingdown?, #wait_for_termination
Methods included from Executor
Constructor Details
#initialize(opts = {}) ⇒ JavaCachedThreadPool
Create a new thread pool.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/concurrent/executor/java_cached_thread_pool.rb', line 18 def initialize(opts = {}) @overflow_policy = opts.fetch(:overflow_policy, :abort) @max_queue = 0 raise ArgumentError.new("#{@overflow_policy} is not a valid overflow policy") unless OVERFLOW_POLICIES.keys.include?(@overflow_policy) @executor = java.util.concurrent.Executors.newCachedThreadPool @executor.setRejectedExecutionHandler(OVERFLOW_POLICIES[@overflow_policy].new) set_shutdown_hook end |