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?

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



161
162
163
164
# File 'lib/concurrent/executor/executor.rb', line 161

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 executor is not running.



216
217
218
219
# File 'lib/concurrent/executor/executor.rb', line 216

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



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/concurrent/executor/executor.rb', line 144

def post(*args)
  raise ArgumentError.new('no block given') unless block_given?
  if running?
    @executor.submit{ yield(*args) }
    true
  else
    false
  end
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
  raise RejectedExecutionError
end

#running?Boolean

Is the executor running?

Returns:

  • (Boolean)

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



169
170
171
# File 'lib/concurrent/executor/executor.rb', line 169

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 executor is not running.



207
208
209
210
# File 'lib/concurrent/executor/executor.rb', line 207

def shutdown
  @executor.shutdown
  nil
end

#shutdown?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

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



187
188
189
# File 'lib/concurrent/executor/executor.rb', line 187

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

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

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



176
177
178
179
180
181
182
# File 'lib/concurrent/executor/executor.rb', line 176

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

#wait_for_termination(timeout) ⇒ 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)

    the maximum number of seconds to wait for shutdown to complete

Returns:

  • (Boolean)

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



200
201
202
# File 'lib/concurrent/executor/executor.rb', line 200

def wait_for_termination(timeout)
  @executor.awaitTermination(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
end