Module: Concurrent::JavaExecutor

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

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



228
229
230
231
# File 'lib/concurrent/executor/executor.rb', line 228

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.



269
270
271
272
# File 'lib/concurrent/executor/executor.rb', line 269

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



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/concurrent/executor/executor.rb', line 214

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

#running?Boolean

Is the executor running?

Returns:

  • (Boolean)

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



234
235
236
# File 'lib/concurrent/executor/executor.rb', line 234

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.



263
264
265
266
# File 'lib/concurrent/executor/executor.rb', line 263

def shutdown
  @executor.shutdown
  nil
end

#shutdown?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

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



248
249
250
# File 'lib/concurrent/executor/executor.rb', line 248

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

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

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



239
240
241
242
243
244
245
# File 'lib/concurrent/executor/executor.rb', line 239

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`



253
254
255
256
257
258
259
260
# File 'lib/concurrent/executor/executor.rb', line 253

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