Promise of a better
future
Pooja Akshantal
Rahul Goma Phulore
Who are we?
What we are going to talk about
• How concurrency and parallelism are different
ideas
• Why concurrency is important
• Fut...
Concurrency ≠ Parallelism
Concurrency is a program-
structuring technique in which
there are multiple threads of
control
A...
Orthogonality illustrated
Javascript
promises, Python
generators
Java and Scala
futures
Well… lots of
things
Parallel
coll...
Fall of Moore’s law
Age of multicore
• Multicore has become a norm!
• Need to exploit parallelism to perform
• Good concurrency abstractions a...
Futures and promises
Futures and promises
• Can be thought of as a single concurrency
abstraction
Future ≈ Cheap thread
• Futures are multiplexed dynamically onto
threads as needed.
• Managed by ExecutionContext.
• It’s ...
Code Time
Async and non-blocking
• Goal: Do not block current thread while
waiting for the result of the future.
• Callbacks:
– Regi...
Callbacks?!
There is a solution!
• Higher-order functions / combinators
• for-comprehensions
• Other abstractions
Running example
• Collect statistics for a
facebook page
• For every post on the
page:
– Collect all likes
– Collect all c...
Code Time
Other parallels
• Error recovery combinators
– recover
– recoverWith
• Collection operations
– map: (Seq[A], A ⇒ B) ⇒ Seq[...
Pitfalls
• Interactions with thread-unsafe libraries
• ThreadLocal variables
• Retries, time-outs not very natural
• Simpl...
Credits
• Heather Miller, for her material on
relationship between futures and promises
• Simon Marlow and Rob Pike, for t...
Thank You!
☺
Upcoming SlideShare
Loading in …5
×

Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

3,125 views
3,051 views

Published on

With the recent, vivid trend towards multicore hardware and the ever growing application requirements, concurrency is no more a niche area it used to be, and is slowly becoming a norm. In this talk, we will talk about promises/futures, one of the concurrency models that has risen to the occasion. We will look at what they are, how they're implemented and used in Java and Javascript. We will see how Scala, with its functional paradigm and greater abstraction capabilities, avoids "callback hell" typically associated with the model, allows writing of concurrent code in "direct style", and thereby greatly reduces the cognitive burden, allowing you to focus on application logic better.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,125
On SlideShare
0
From Embeds
0
Number of Embeds
1,578
Actions
Shares
0
Downloads
14
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

  1. 1. Promise of a better future Pooja Akshantal Rahul Goma Phulore
  2. 2. Who are we?
  3. 3. What we are going to talk about • How concurrency and parallelism are different ideas • Why concurrency is important • Futures and promises • DEMO! • Pitfalls
  4. 4. Concurrency ≠ Parallelism Concurrency is a program- structuring technique in which there are multiple threads of control A parallel program is one that uses a multiplicity of computational hardware (e.g. multiple processor cores) Structure Execution Dealing with lots of things at once Doing lots of things at once
  5. 5. Orthogonality illustrated Javascript promises, Python generators Java and Scala futures Well… lots of things Parallel collections
  6. 6. Fall of Moore’s law
  7. 7. Age of multicore • Multicore has become a norm! • Need to exploit parallelism to perform • Good concurrency abstractions a way to get there
  8. 8. Futures and promises
  9. 9. Futures and promises • Can be thought of as a single concurrency abstraction
  10. 10. Future ≈ Cheap thread • Futures are multiplexed dynamically onto threads as needed. • Managed by ExecutionContext. • It’s practical to have hundreds of thousands going at once. • Spawn away!
  11. 11. Code Time
  12. 12. Async and non-blocking • Goal: Do not block current thread while waiting for the result of the future. • Callbacks: – Register callback which is invoked asynchronously when future is completed – Async computations do not block
  13. 13. Callbacks?!
  14. 14. There is a solution! • Higher-order functions / combinators • for-comprehensions • Other abstractions
  15. 15. Running example • Collect statistics for a facebook page • For every post on the page: – Collect all likes – Collect all comments • Aggregate results into popularity score
  16. 16. Code Time
  17. 17. Other parallels • Error recovery combinators – recover – recoverWith • Collection operations – map: (Seq[A], A ⇒ B) ⇒ Seq[B] mapF: (Seq[A], A ⇒ Future[B]) ⇒ Future[Seq[B]] – filter: (Seq[A], A ⇒ Boolean) ⇒ Seq[A] filterF: (Seq[A], A ⇒ Future[Boolean]) ⇒ Future[Seq[A]]
  18. 18. Pitfalls • Interactions with thread-unsafe libraries • ThreadLocal variables • Retries, time-outs not very natural • Simpler than other concurrency models in many ways, but still incur some cognitive cost
  19. 19. Credits • Heather Miller, for her material on relationship between futures and promises • Simon Marlow and Rob Pike, for their material on concurrency and parallelism • Josh Suereth, for his Github example (Inspiration for our facebook example) • Scala, Play, and Akka teams for all their amazing work
  20. 20. Thank You! ☺

×