Class: Concurrent::RubyCachedThreadPool

Inherits:
RubyThreadPoolExecutor show all
Defined in:
lib/concurrent/executor/ruby_cached_thread_pool.rb

Overview

Note:

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`

Direct Known Subclasses

CachedThreadPool

Constant Summary

Constants inherited from RubyThreadPoolExecutor

Concurrent::RubyThreadPoolExecutor::DEFAULT_MAX_POOL_SIZE, Concurrent::RubyThreadPoolExecutor::DEFAULT_MAX_QUEUE_SIZE, Concurrent::RubyThreadPoolExecutor::DEFAULT_MIN_POOL_SIZE, Concurrent::RubyThreadPoolExecutor::DEFAULT_THREAD_IDLETIMEOUT, Concurrent::RubyThreadPoolExecutor::OVERFLOW_POLICIES

Instance Attribute Summary

Attributes inherited from RubyThreadPoolExecutor

#completed_task_count, #idletime, #largest_length, #max_length, #max_queue, #min_length, #overflow_policy, #scheduled_task_count

Instance Method Summary collapse

Methods inherited from RubyThreadPoolExecutor

#can_overflow?, #length, #queue_length, #remaining_capacity, #status

Methods included from RubyExecutor

#<<, #kill, #post, #running?, #shutdown, #shutdown?, #shuttingdown?, #wait_for_termination

Methods included from Logging

#log

Methods included from Executor

#can_overflow?

Constructor Details

#initialize(opts = {}) ⇒ RubyCachedThreadPool

Create a new thread pool.

Parameters:

  • opts (Hash) (defaults to: {})

    the options defining pool behavior. number of seconds a thread may be idle before it is reclaimed

Raises:

  • (ArgumentError)

    if ‘overflow_policy` is not a known policy



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/concurrent/executor/ruby_cached_thread_pool.rb', line 14

def initialize(opts = {})
  overflow_policy = opts.fetch(:overflow_policy, :abort)

  raise ArgumentError.new("#{overflow_policy} is not a valid overflow policy") unless OVERFLOW_POLICIES.include?(overflow_policy)

  opts = opts.merge(
    min_threads: 0,
    max_threads: DEFAULT_MAX_POOL_SIZE,
    num_threads: overflow_policy,
    max_queue: DEFAULT_MAX_QUEUE_SIZE,
    idletime: DEFAULT_THREAD_IDLETIMEOUT
  )
  super(opts)
end