Class: Concurrent::JavaFixedThreadPool

Inherits:
JavaThreadPoolExecutor show all
Defined in:
lib/concurrent/executor/java_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`

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

Methods inherited from JavaThreadPoolExecutor

#can_overflow?, #completed_task_count, #idletime, #largest_length, #length, #min_length, #queue_length, #remaining_capacity, #running?, #scheduled_task_count, #shutdown, #status

Methods included from JavaExecutor

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

Methods included from Executor

#can_overflow?

Constructor Details

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

Create a new thread pool.

Parameters:

  • 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

See Also:



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

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

  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.keys.include?(@overflow_policy)

  @executor = java.util.concurrent.Executors.newFixedThreadPool(num_threads)
  @executor.setRejectedExecutionHandler(OVERFLOW_POLICIES[@overflow_policy].new)

  set_shutdown_hook
end