Module: Concurrent::Edge::FutureShortcuts

Defined in:
lib/concurrent/edge/future.rb

Overview

Provides edge features, which will be added to or replace features in main gem.

Contains new unified implementation of Futures and Promises which combines Features of previous Future, Promise, IVar, Event, Probe, dataflow, Delay, TimerTask into single framework. It uses extensively new synchronization layer to make all the paths lock-free with exception of blocking threads on #wait. It offers better performance and does not block threads (exception being #wait and similar methods where it’s intended).

## Examples

Instance Method Summary collapse

Instance Method Details

#any_complete(*futures) ⇒ Future Also known as: any

Constructs new Concurrent::Edge::Future which is completed after first of the futures is complete.



101
102
103
# File 'lib/concurrent/edge/future.rb', line 101

def any_complete(*futures)
  AnyCompletePromise.new(futures, :io).future
end

#any_successful(*futures) ⇒ Future

Constructs new Concurrent::Edge::Future which becomes succeeded after first of the futures succeedes or failed if all futures fail (reason is last error).



111
112
113
# File 'lib/concurrent/edge/future.rb', line 111

def any_successful(*futures)
  AnySuccessfulPromise.new(futures, :io).future
end

#completed_event(default_executor = :io) ⇒ Event



59
60
61
# File 'lib/concurrent/edge/future.rb', line 59

def completed_event(default_executor = :io)
  ImmediateEventPromise.new(default_executor).event
end

#completed_future(success, value, reason, default_executor = :io) ⇒ Future



44
45
46
# File 'lib/concurrent/edge/future.rb', line 44

def completed_future(success, value, reason, default_executor = :io)
  ImmediateFuturePromise.new(default_executor, success, value, reason).future
end

#delay(default_executor = :io, &task) ⇒ Future

Constructs new Future which will evaluate to the block after requested by calling #wait, #value, #value!, etc. on it or on any of the chained futures.



68
69
70
# File 'lib/concurrent/edge/future.rb', line 68

def delay(default_executor = :io, &task)
  DelayPromise.new(default_executor).future.then(&task)
end

#event(default_executor = :io) ⇒ CompletableEvent

User is responsible for completing the event once by CompletableEvent#complete



25
26
27
# File 'lib/concurrent/edge/future.rb', line 25

def event(default_executor = :io)
  CompletableEventPromise.new(default_executor).future
end

#failed_future(reason, default_executor = :io) ⇒ Future



54
55
56
# File 'lib/concurrent/edge/future.rb', line 54

def failed_future(reason, default_executor = :io)
  completed_future false, nil, reason, default_executor
end

#future(default_executor = :io, &task) ⇒ Future #future(default_executor = :io) ⇒ CompletableFuture Also known as: async

Overloads:



35
36
37
38
39
40
41
# File 'lib/concurrent/edge/future.rb', line 35

def future(default_executor = :io, &task)
  if task
    ImmediateEventPromise.new(default_executor).future.then(&task)
  else
    CompletableFuturePromise.new(default_executor).future
  end
end

#post(*args, &job) ⇒ true, false

post job on :io executor



136
137
138
# File 'lib/concurrent/edge/future.rb', line 136

def post(*args, &job)
  post_on(:io, *args, &job)
end

#post!(*args, &job) ⇒ true, false

post job on :fast executor



130
131
132
# File 'lib/concurrent/edge/future.rb', line 130

def post!(*args, &job)
  post_on(:fast, *args, &job)
end

#post_on(executor, *args, &job) ⇒ true, false

post job on executor



142
143
144
# File 'lib/concurrent/edge/future.rb', line 142

def post_on(executor, *args, &job)
  Concurrent.executor(executor).post(*args, &job)
end

#schedule(intended_time, default_executor = :io, &task) ⇒ Future

Schedules the block to be executed on executor in given intended_time.



75
76
77
# File 'lib/concurrent/edge/future.rb', line 75

def schedule(intended_time, default_executor = :io, &task)
  ScheduledPromise.new(default_executor, intended_time).future.then(&task)
end

#select(*channels) ⇒ Future

only proof of concept



117
118
119
120
121
122
123
124
125
126
# File 'lib/concurrent/edge/future.rb', line 117

def select(*channels)
  future do
    # noinspection RubyArgCount
    Channel.select do |s|
      channels.each do |ch|
        s.take(ch) { |value| [value, ch] }
      end
    end
  end
end

#succeeded_future(value, default_executor = :io) ⇒ Future



49
50
51
# File 'lib/concurrent/edge/future.rb', line 49

def succeeded_future(value, default_executor = :io)
  completed_future true, value, nil, default_executor
end

#zip_events(*futures_and_or_events) ⇒ Event

Constructs new Event which is completed after all futures_and_or_events are complete (Future is completed when Success or Failed).



94
95
96
# File 'lib/concurrent/edge/future.rb', line 94

def zip_events(*futures_and_or_events)
  ZipEventsPromise.new(futures_and_or_events, :io).future
end

#zip_futures(*futures_and_or_events) ⇒ Future Also known as: zip

Constructs new Concurrent::Edge::Future which is completed after all futures_and_or_events are complete. Its value is array of dependent future values. If there is an error it fails with the first one. Event does not have a value so it’s represented by nil in the array of values.



84
85
86
# File 'lib/concurrent/edge/future.rb', line 84

def zip_futures(*futures_and_or_events)
  ZipFuturesPromise.new(futures_and_or_events, :io).future
end