Class: Concurrent::JavaSingleThreadExecutor

Inherits:
Object
  • Object
show all
Includes:
JavaExecutor, SerialExecutor
Defined in:
lib/concurrent/executor/java_single_thread_executor.rb

Overview

Note:

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

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 SingleThreadExecutor

Constant Summary

Constants included from JavaExecutor

Concurrent::JavaExecutor::FALLBACK_POLICIES

Instance Attribute Summary

Attributes included from Executor

#fallback_policy

Instance Method Summary collapse

Methods included from SerialExecutor

#serialized?

Methods included from Executor

#can_overflow?, #serialized?

Methods included from JavaExecutor

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

Constructor Details

#initialize(opts = {}) ⇒ JavaSingleThreadExecutor

Create a new thread pool.

Options Hash (opts):

  • :fallback_policy (Symbol) — default: :discard

    the policy for handling new tasks that are received when the queue size has reached max_queue or after the executor has shut down

Raises:

  • (ArgumentError)

See Also:



20
21
22
23
24
25
# File 'lib/concurrent/executor/java_single_thread_executor.rb', line 20

def initialize(opts = {})
  @executor = java.util.concurrent.Executors.newSingleThreadExecutor
  @fallback_policy = opts.fetch(:fallback_policy, :discard)
  raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICIES.keys.include?(@fallback_policy)
  set_shutdown_hook
end