Class: Concurrent::RubyFixedThreadPool

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

Overview

Note:

When running on the JVM (JRuby) this class will inherit from JavaFixedThreadPool. On all other platforms it will inherit from RubyFixedThreadPool.

A thread pool with a set number of threads. The number of threads in the pool is set on construction and remains constant. When all threads are busy new tasks #post to the thread pool are enqueued until a thread becomes available. Should a thread crash for any reason the thread will immediately be removed from the pool and replaced.

The API and behavior of this class are based on Java’s FixedThreadPool

Direct Known Subclasses

FixedThreadPool

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?, #serialized?

Constructor Details

#initialize(num_threads, opts = {}) ⇒ RubyFixedThreadPool

Create a new thread pool.

Parameters:

  • num_threads (Integer)

    the number of threads to allocate

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

    the options defining pool behavior.

Options Hash (opts):

  • :overflow_policy (Symbol) — default: `:abort`

    the overflow policy

Raises:

  • (ArgumentError)

    if num_threads is less than or equal to zero

  • (ArgumentError)

    if overflow_policy is not a known policy



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/concurrent/executor/ruby_fixed_thread_pool.rb', line 16

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

  raise ArgumentError.new('number of threads must be greater than zero') if num_threads < 1
  raise ArgumentError.new("#{overflow_policy} is not a valid overflow policy") unless OVERFLOW_POLICIES.include?(overflow_policy)

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