Module: Concurrent::JavaExecutor

Includes:
Executor
Included in:
JavaSingleThreadExecutor, JavaThreadPoolExecutor
Defined in:
lib/concurrent/executor/executor.rb

Constant Summary collapse

FALLBACK_POLICIES =

The set of possible fallback policies that may be set at thread pool creation.

{
  abort: java.util.concurrent.ThreadPoolExecutor::AbortPolicy,
  discard: java.util.concurrent.ThreadPoolExecutor::DiscardPolicy,
  caller_runs: java.util.concurrent.ThreadPoolExecutor::CallerRunsPolicy
}.freeze

Instance Attribute Summary

Attributes included from Executor

#fallback_policy

Instance Method Summary collapse

Methods included from Executor

#can_overflow?, #serialized?

Instance Method Details

#<<(task) ⇒ self

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



266
267
268
269
# File 'lib/concurrent/executor/executor.rb', line 266

def <<(task)
  post(&task)
  self
end

#killObject

Begin an immediate shutdown. In-progress tasks will be allowed to complete but enqueued tasks will be dismissed and no new tasks will be accepted. Has no additional effect if the thread pool is not running.



307
308
309
310
# File 'lib/concurrent/executor/executor.rb', line 307

def kill
  @executor.shutdownNow
  nil
end

#post(*args) { ... } ⇒ Boolean

Submit a task to the executor for asynchronous processing.

Parameters:

  • args (Array)

    zero or more arguments to be passed to the task

Yields:

  • the asynchronous task to perform

Returns:

  • (Boolean)

    ‘true` if the task is queued, `false` if the executor is not running

Raises:

  • (ArgumentError)

    if no task is given



255
256
257
258
259
260
261
262
263
# File 'lib/concurrent/executor/executor.rb', line 255

def post(*args, &task)
  raise ArgumentError.new('no block given') unless block_given?
  return handle_fallback(*args, &task) unless running?
  executor_submit = @executor.java_method(:submit, [Runnable.java_class])
  executor_submit.call { yield(*args) }
  true
rescue Java::JavaUtilConcurrent::RejectedExecutionException
  raise RejectedExecutionError
end

#running?Boolean

Is the executor running?

Returns:

  • (Boolean)

    ‘true` when running, `false` when shutting down or shutdown



272
273
274
# File 'lib/concurrent/executor/executor.rb', line 272

def running?
  ! (shuttingdown? || shutdown?)
end

#shutdownObject

Begin an orderly shutdown. Tasks already in the queue will be executed, but no new tasks will be accepted. Has no additional effect if the thread pool is not running.



301
302
303
304
# File 'lib/concurrent/executor/executor.rb', line 301

def shutdown
  @executor.shutdown
  nil
end

#shutdown?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

    ‘true` when shutdown, `false` when shutting down or running



286
287
288
# File 'lib/concurrent/executor/executor.rb', line 286

def shutdown?
  @executor.isShutdown || @executor.isTerminated
end

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

    ‘true` when not running and not shutdown, else `false`



277
278
279
280
281
282
283
# File 'lib/concurrent/executor/executor.rb', line 277

def shuttingdown?
  if @executor.respond_to? :isTerminating
    @executor.isTerminating
  else
    false
  end
end

#wait_for_termination(timeout = nil) ⇒ Boolean

Note:

Does not initiate shutdown or termination. Either ‘shutdown` or `kill` must be called before this method (or on another thread).

Block until executor shutdown is complete or until ‘timeout` seconds have passed.

Parameters:

  • timeout (Integer) (defaults to: nil)

    the maximum number of seconds to wait for shutdown to complete

Returns:

  • (Boolean)

    ‘true` if shutdown complete or false on `timeout`



291
292
293
294
295
296
297
298
# File 'lib/concurrent/executor/executor.rb', line 291

def wait_for_termination(timeout = nil)
  if timeout.nil?
    ok = @executor.awaitTermination(60, java.util.concurrent.TimeUnit::SECONDS) until ok
    true
  else
    @executor.awaitTermination(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
  end
end