Class: Concurrent::Future

Inherits:
Object
  • Object
show all
Includes:
Obligation, UsesGlobalThreadPool, Observable
Defined in:
lib/concurrent/future.rb

Instance Attribute Summary

Attributes included from Obligation

#reason, #state

Instance Method Summary collapse

Methods included from UsesGlobalThreadPool

included

Methods included from Obligation

#fulfilled?, #pending?, #rejected?, #value

Methods included from Dereferenceable

#set_deref_options, #value

Constructor Details

#initialize(*args, &block) ⇒ Future

Returns a new instance of Future.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/concurrent/future.rb', line 14

def initialize(*args, &block)
  unless block_given?
    @state = :fulfilled
  else
    @value = nil
    @state = :pending
    Future.thread_pool.post(*args) do
      work(*args, &block)
    end
  end
end

Instance Method Details

#add_observer(observer, func = :update) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/concurrent/future.rb', line 26

def add_observer(observer, func = :update)
  val = self.value
  mutex.synchronize do
    if event.set?
      Future.thread_pool.post(func, Time.now, val, @reason) do |f, *args|
        observer.send(f, *args)
      end
    else
      super
    end
  end
  return func
end