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
`reason`. Observers added after fulfillment/rejection will still be notified as normal.
with three parameters: the ‘Time` of fulfillment/rejection, the final `value`, and the final
according to the normal `Observable` behavior. The observer callback function will be called
but does so in a thread-safe way. On fulfillment or rejection all observers will be notified
The `Future` class also includes the behavior of the Ruby standard library `Observable` module,
not block. Any other integer or float value will indicate the maximum number of seconds to block.
long the call will block. If `nil` the call will block indefinitely. If `0` the call will
:rejected or :fulfilled. A timeout value can be passed to `#value` to limit how
`Future` is :pending a call to `#value` will block until the `Future` is either
:fulfilled a call to `#value` will immediately return the current value. When a
:rejected a call to `#value` will return `nil` immediately. When a `Future` is
Obtaining the value of a `Future` is a potentially blocking operation. When a `Future` is
Retrieving the value of a `Future` is done through the `#value` (alias: `#deref`) method.
obtain the state of the `Future`, as can the `#state` method, which returns a symbol.
`#unscheduled?`, `#pending?`, `#rejected?`, and `fulfilled?` can be called at any time to
`reason` will be updated with a reference to the thrown exception. The predicate methods
its `value` will be updated to reflect the result of the operation. If :rejected the
thrown during processing, or :fulfilled, indicating success. If a `Future` is :fulfilled
complete. A completed `Future` is either :rejected, indicating that an exception was
called the state becomes :pending and will remain in that state until processing is
When a `Future` is created its state is set to :unscheduled. Once the `#execute` method is
A `Future` has four possible states: :unscheduled, :pending, :rejected, or :fulfilled.
of the async operation at a later time.
for asynchronous completion, do other stuff, then return and retrieve the result
The action is atomic and permanent. The idea behind a future is to send an operation
A `Future` represents a promise to complete an action at some time in the future.
def self.execute(opts = {}, &block)
- Since: - 0.5.0
Raises:
-
(ArgumentError)
- if no block is given
Returns:
-
(Future)
- the newly created `Future` in the `:pending` state
Options Hash:
(**opts)
-
:copy_on_deref
(String
) -- call the given `Proc` passing the internal value and -
:freeze_on_deref
(String
) -- call `#freeze` before returning the data -
:dup_on_deref
(String
) -- call `#dup` before returning the data
Other tags:
- Yield: - the asynchronous operation to perform
def self.execute(opts = {}, &block) Future.new(opts, &block).execute end
def execute
- Since: - 0.5.0
Other tags:
- 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{ work } self end end
def initialize(opts = {}, &block)
-
(ArgumentError)
- if no block is given
Options Hash:
(**opts)
-
:copy_on_deref
(String
) -- call the given `Proc` passing the internal value and -
:freeze_on_deref
(String
) -- call `#freeze` before returning the data -
:dup_on_deref
(String
) -- call `#dup` before returning the data -
:executor
(object
) -- when provided will run all operations on -
:operation
(Boolean
) -- when `true` will execute the future on the global
Parameters:
-
opts
(Hash
) -- the options controlling how the future will be processed
Other tags:
- Yield: - the asynchronous operation to perform
def initialize(opts = {}, &block) raise ArgumentError.new('no block given') unless block_given? super(IVar::NO_VALUE, opts) @state = :unscheduled @task = block @executor = OptionsParser::get_executor_from(opts) end
def work # :nodoc:
@!visibility private
def work # :nodoc: success, val, reason = SafeTaskExecutor.new(@task).execute complete(success, val, reason) end