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.



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.

Yields:

  • the asynchronous task to perform

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?



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?



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

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

#shuttingdown?Boolean

Is the executor shuttingdown?



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.



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