class Concurrent::Future
@see docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html java.util.concurrent.Future
@see clojuredocs.org/clojure_core/clojure.core/future Clojure’s future function
@see ruby-doc.org/stdlib-2.1.1/libdoc/observer/rdoc/Observable.html Ruby Observable module
@!macro copy_options
{include:file:docs-source/future.md}
def self.execute(opts = {}, &block)
-
(Future)
- the newly created `Future` in the `:pending` state
Raises:
-
(ArgumentError)
- if no block is given
Options Hash:
(**opts)
-
:args
(object, Array
) -- zero or more arguments to be passed the task
Other tags:
- Yield: - the asynchronous operation to perform
def self.execute(opts = {}, &block) Future.new(opts, &block).execute end
def cancel
-
(Boolean)
- was the operation successfully cancelled.
def cancel if compare_and_set_state(:cancelled, :pending) complete(false, nil, CancelledOperationError.new) true else false end end
def cancelled?
-
(Boolean)
-
def cancelled? state == :cancelled end
def execute
- Example: Instance and execute in one line -
Example: Instance and execute in separate steps -
Returns:
-
(Future)
- a reference to `self`
def execute if compare_and_set_state(:pending, :unscheduled) @executor.post{ safe_execute(@task, @args) } self end end
def initialize(opts = {}, &block)
-
(ArgumentError)
- if no block is given
Options Hash:
(**opts)
-
:args
(object, Array
) -- zero or more arguments to be passed the task
Other tags:
- Yield: - the asynchronous operation to perform
def initialize(opts = {}, &block) raise ArgumentError.new('no block given') unless block_given? super(NULL, opts.merge(__task_from_block__: block), &nil) end
def ns_initialize(value, opts)
def ns_initialize(value, opts) super @state = :unscheduled @task = opts[:__task_from_block__] @executor = Options.executor_from_options(opts) || Concurrent.global_io_executor @args = get_arguments_from(opts) end
def set(value = NULL, &block)
def set(value = NULL, &block) check_for_block_or_value!(block_given?, value) synchronize do if @state != :unscheduled raise MultipleAssignmentError else @task = block || Proc.new { value } end end execute end
def wait_or_cancel(timeout)
-
(Boolean)
- true if the operation completed before the timeout
Parameters:
-
timeout
(Numeric
) -- the maximum time in seconds to wait.
def wait_or_cancel(timeout) wait(timeout) if complete? true else cancel false end end