SlideShare a Scribd company logo
1 of 45
Download to read offline
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
YIFAN XING - 2018
ASYNCHRONOUS
PROGRAMMING
Yifan Xing
SCALA.CONCURRENT AND
MONIX!
@yifan_xing_e
01 Synchronous vs. Asynchronous
Differences
Futures & Promises
Debugging Async Programs
Monix
A TOUR OF ASYNCHRONOUS
PROGRAMMING IN SCALA
02
03
04
05Overview
ASYNCHRONOUS
PROGRAMMING:
SCALA.CONCURRENT
AND MONIX!
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Concurrency
Synchronous
Asynchronous
Single-threaded
Multi-threaded
YIFAN XING - 2018 @yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Synchronous: Single-threaded
YIFAN XING - 2018
Task 1 Task 2 Task 3
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Synchronous: Multi-threaded
YIFAN XING - 2018
Task 1
Task 2
Task 3
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Asynchronous: Single-threaded
YIFAN XING - 2018
Task 1
Task 2
Task 3
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Asynchronous: Multi-threaded
YIFAN XING - 2018
Task 1
Task 2
Task 3
@yifan_xing_e
Future
Promise
ExecutionContext
Examples
Scala.concurrent
YIFAN XING - 2018
Scala.concurrent: Future
Computation Incomplete:
Computation Completed:
Future is not completed
Future is completed: with a Value => Success
Future is completed: with an Exception => Failure
An object holding a value which may become available at some point
Immutable
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALAA TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
Future
YIFAN XING - 2018
Example: Callbacks
Multiple
callbacks may
be registered;
NO guarantee
that they will be
executed in a
particular order.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Example: Callbacks
Chain callbacks
Allow one to enforce
callbacks to be
executed in a
specified order.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Example: Callbacks
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
Exception in
callback:
NOT propagated
to the
subsequent
andThen
callbacks.
YIFAN XING - 2018
Example: Transformations
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Example: Transformations
More generic &
flexible
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Example: Transformations
If no match
cases or original future
contains a valid result:
New future will contain
the same result as the
original one.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALAA TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
Evaluated only after
a Failure
YIFAN XING - 2018
Example: Transformations
If both futures failed,
the resulting future
holds the throwable
object of the first
future.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Scala.concurrent: Promises
A writable, single-assignment container
Can create a Future
Can complete a Future, only ONCE
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
Promise:
YIFAN XING - 2018
Example: Promises
A Promise can be
completed at most ONCE.
If the promise has already
been fulfilled, failed or
has timed out, calling this
method will throw an
IllegalStateException.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
Example: Promises
If the promise has
already been
completed returns
false, or true
otherwise.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
@yifan_xing_e
YIFAN XING - 2018
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Problem in A Client-facing Interface
@yifan_xing_e
YIFAN XING - 2018
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
p.future
Problem in A Client-facing Interface
@yifan_xing_e
YIFAN XING - 2018
Monix
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
What is Monix?
Asynchronous, event-based programs
Data type: Task, etc.
"Factory" of Future instances
Lazy & asynchronous computations
YIFAN XING - 2018 @yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: Task
YIFAN XING - 2018
Lazily evaluated
Evaluated when
call runAsync
Trigger side effects
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: Task Callback
YIFAN XING - 2018
Eagerly evaluate task
Register callbacks
Similar to
Future#onComplete
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: Eager/ Lazy
YIFAN XING - 2018
Evaluate eagerly
Equivalent to Future#successful
Defer eager evaluation
Similar to Task#eval
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: Memoization
YIFAN XING - 2018
Evaluated lazily
Not memoized, recompute each
time the Task is executed.
Evaluate lazily
Evaluated exactly ONCE
Result is memoized
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: MemoizeOnSuccess
YIFAN XING - 2018
Exceptions are not cached.
Only cache the first
successful result
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: Alternative Thread Pool
YIFAN XING - 2018
Overrides default scheduler
Overriding the "default"
scheduler can only happen
ONCE
Task is immutable
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Monix: CancelableFuture
YIFAN XING - 2018
Calling it multiple
times has the same side-
effect as calling it only once.
@yifan_xing_e
- A value
- Executed when constructed
- Eager evaluation
- ExecutionContext needed whenever use   
   operators
A function - 
Execution controllable by user - 
Lazy evaluation - 
ExecutionScheduler needed whenever - 
 use runAsync   
MonixFuture
Eager/Lazy
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
- Memoized by default
- Evaluated once
- Store result
Explicitly use memoization operators - 
Evaluated whenever needed - 
Referential transparency - 
MonixFuture
Memoization
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
- Sometimes errors are                         
   not propagated
Scheduler.reportFailure - 
Defaults to System.err - 
Allow customized logging tools - 
MonixFuture
ErrorHandling
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Future Monix
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
FutureMonix
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Debugging
BEST PRACTICES
In Asynchronous Programs
@yifan_xing_e
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Writing Async Code
1. Rethink before
adding more code
2. Adding more code to
fix problems will
usually add more bugs
D e b u g
1. Test code in isolation
2. Observe what is
happening
3. Confirm behavior
before moving on
T e s t
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Using Await & Print
YIFAN XING - 2018 @yifan_xing_e
TimeoutException if after
waiting for the specified
time awaitable is still not
ready
atMost may be:
A finite positive duration
Negative (no waiting is
done)
Duration.Inf for
unbounded waiting
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
ScalaTest: ScalaFutures
YIFAN XING - 2018 @yifan_xing_e
Default timeout
150ms
TestFailedException
if not finished after
timeout
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Resources: Async Debugger & Capture
YIFAN XING - 2018 @yifan_xing_e
IntelliJ Capture (2017):
Iulian Dragos - Async Debugger in Scala IDE
IntelliJ IDEA 2017.1 EAP Extends Debugger with Async Stacktraces
Developer's Explanation
Debugging Tutorial
Async Stacktraces
Stack Retention
Scala IDE Doc
Demo: Jamie Allen
Rethink the Debugger: Scala Days 2014
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
Using Async Debugger / Capture
YIFAN XING - 2018 @yifan_xing_e
Substituting its parts related
to the asynchronous code
execution with where it was
called
Sender: Async code executor
Receiver: executed code
Config exact signatures of
methods of the async code
Stores the stack and
variables info in a map
(1000 stacks), where the
key is a parameter with
the specified number.
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
YIFAN XING - 2018 @yifan_xing_e
( Sorry Alex :P )
A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
YIFAN XING - 2018 @yifan_xing_e
T H A N K Y O U
@yifan_xing_e

More Related Content

Similar to Asynchronous programming 20180607

SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진VMware Tanzu Korea
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeMário Almeida
 
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Christian Catalan
 
Configure active directory & trust domain
Configure active directory & trust domainConfigure active directory & trust domain
Configure active directory & trust domainTola LENG
 
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) [Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) Johnny Sung
 
Testing Adhearsion Applications
Testing Adhearsion ApplicationsTesting Adhearsion Applications
Testing Adhearsion ApplicationsLuca Pradovera
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannEclipse Day India
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisRuslan Shevchenko
 
Improvements to Flink & it's Applications in Alibaba Search
Improvements to Flink & it's Applications in Alibaba SearchImprovements to Flink & it's Applications in Alibaba Search
Improvements to Flink & it's Applications in Alibaba SearchDataWorks Summit/Hadoop Summit
 
Consensus algorithms_PapersWeLove2018Sep
Consensus algorithms_PapersWeLove2018SepConsensus algorithms_PapersWeLove2018Sep
Consensus algorithms_PapersWeLove2018SepYifan Xing
 
Consensus Algorithms in Distributed Systems LambdaWorld2018
Consensus Algorithms in Distributed Systems LambdaWorld2018Consensus Algorithms in Distributed Systems LambdaWorld2018
Consensus Algorithms in Distributed Systems LambdaWorld2018Yifan Xing
 
Building a social network in under 4 weeks with Serverless and GraphQL
Building a social network in under 4 weeks with Serverless and GraphQLBuilding a social network in under 4 weeks with Serverless and GraphQL
Building a social network in under 4 weeks with Serverless and GraphQLYan Cui
 
Spryker Hackathon Q1 2016
Spryker Hackathon Q1 2016Spryker Hackathon Q1 2016
Spryker Hackathon Q1 2016Fabian Wesner
 
Efficient Django
Efficient DjangoEfficient Django
Efficient DjangoDavid Arcos
 
Build social network in 4 weeks
Build social network in 4 weeksBuild social network in 4 weeks
Build social network in 4 weeksYan Cui
 
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"LogeekNightUkraine
 
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...Florian Reifschneider
 
Caffe - A deep learning framework (Ramin Fahimi)
Caffe - A deep learning framework (Ramin Fahimi)Caffe - A deep learning framework (Ramin Fahimi)
Caffe - A deep learning framework (Ramin Fahimi)irpycon
 
Solving Nonograms In Parallel
Solving Nonograms In ParallelSolving Nonograms In Parallel
Solving Nonograms In Parallel家郡 葉
 

Similar to Asynchronous programming 20180607 (20)

SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
 
Configure active directory & trust domain
Configure active directory & trust domainConfigure active directory & trust domain
Configure active directory & trust domain
 
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) [Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
 
Testing Adhearsion Applications
Testing Adhearsion ApplicationsTesting Adhearsion Applications
Testing Adhearsion Applications
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
Improvements to Flink & it's Applications in Alibaba Search
Improvements to Flink & it's Applications in Alibaba SearchImprovements to Flink & it's Applications in Alibaba Search
Improvements to Flink & it's Applications in Alibaba Search
 
Consensus algorithms_PapersWeLove2018Sep
Consensus algorithms_PapersWeLove2018SepConsensus algorithms_PapersWeLove2018Sep
Consensus algorithms_PapersWeLove2018Sep
 
Consensus Algorithms in Distributed Systems LambdaWorld2018
Consensus Algorithms in Distributed Systems LambdaWorld2018Consensus Algorithms in Distributed Systems LambdaWorld2018
Consensus Algorithms in Distributed Systems LambdaWorld2018
 
Building a social network in under 4 weeks with Serverless and GraphQL
Building a social network in under 4 weeks with Serverless and GraphQLBuilding a social network in under 4 weeks with Serverless and GraphQL
Building a social network in under 4 weeks with Serverless and GraphQL
 
Spryker Hackathon Q1 2016
Spryker Hackathon Q1 2016Spryker Hackathon Q1 2016
Spryker Hackathon Q1 2016
 
Efficient Django
Efficient DjangoEfficient Django
Efficient Django
 
Build social network in 4 weeks
Build social network in 4 weeksBuild social network in 4 weeks
Build social network in 4 weeks
 
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
 
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...
Angular Frankfurt #1 - Managing Application State in Reactive Angular Applica...
 
Caffe - A deep learning framework (Ramin Fahimi)
Caffe - A deep learning framework (Ramin Fahimi)Caffe - A deep learning framework (Ramin Fahimi)
Caffe - A deep learning framework (Ramin Fahimi)
 
Solving Nonograms In Parallel
Solving Nonograms In ParallelSolving Nonograms In Parallel
Solving Nonograms In Parallel
 

Recently uploaded

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Asynchronous programming 20180607

  • 1. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA YIFAN XING - 2018 ASYNCHRONOUS PROGRAMMING Yifan Xing SCALA.CONCURRENT AND MONIX! @yifan_xing_e
  • 2. 01 Synchronous vs. Asynchronous Differences Futures & Promises Debugging Async Programs Monix A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA 02 03 04 05Overview ASYNCHRONOUS PROGRAMMING: SCALA.CONCURRENT AND MONIX!
  • 3. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Concurrency Synchronous Asynchronous Single-threaded Multi-threaded YIFAN XING - 2018 @yifan_xing_e
  • 4. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Synchronous: Single-threaded YIFAN XING - 2018 Task 1 Task 2 Task 3 @yifan_xing_e
  • 5. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Synchronous: Multi-threaded YIFAN XING - 2018 Task 1 Task 2 Task 3 @yifan_xing_e
  • 6. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Asynchronous: Single-threaded YIFAN XING - 2018 Task 1 Task 2 Task 3 @yifan_xing_e
  • 7. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Asynchronous: Multi-threaded YIFAN XING - 2018 Task 1 Task 2 Task 3 @yifan_xing_e
  • 9. YIFAN XING - 2018 Scala.concurrent: Future Computation Incomplete: Computation Completed: Future is not completed Future is completed: with a Value => Success Future is completed: with an Exception => Failure An object holding a value which may become available at some point Immutable A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALAA TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e Future
  • 10. YIFAN XING - 2018 Example: Callbacks Multiple callbacks may be registered; NO guarantee that they will be executed in a particular order. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 11. YIFAN XING - 2018 Example: Callbacks Chain callbacks Allow one to enforce callbacks to be executed in a specified order. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 12. YIFAN XING - 2018 Example: Callbacks A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e Exception in callback: NOT propagated to the subsequent andThen callbacks.
  • 13. YIFAN XING - 2018 Example: Transformations A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 14. YIFAN XING - 2018 Example: Transformations More generic & flexible A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 15. YIFAN XING - 2018 Example: Transformations If no match cases or original future contains a valid result: New future will contain the same result as the original one. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALAA TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e Evaluated only after a Failure
  • 16. YIFAN XING - 2018 Example: Transformations If both futures failed, the resulting future holds the throwable object of the first future. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 17. YIFAN XING - 2018 Scala.concurrent: Promises A writable, single-assignment container Can create a Future Can complete a Future, only ONCE A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e Promise:
  • 18. YIFAN XING - 2018 Example: Promises A Promise can be completed at most ONCE. If the promise has already been fulfilled, failed or has timed out, calling this method will throw an IllegalStateException. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 19. YIFAN XING - 2018 Example: Promises If the promise has already been completed returns false, or true otherwise. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA @yifan_xing_e
  • 20. YIFAN XING - 2018 A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Problem in A Client-facing Interface @yifan_xing_e
  • 21. YIFAN XING - 2018 A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA p.future Problem in A Client-facing Interface @yifan_xing_e
  • 23. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA What is Monix? Asynchronous, event-based programs Data type: Task, etc. "Factory" of Future instances Lazy & asynchronous computations YIFAN XING - 2018 @yifan_xing_e
  • 24. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: Task YIFAN XING - 2018 Lazily evaluated Evaluated when call runAsync Trigger side effects @yifan_xing_e
  • 25. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: Task Callback YIFAN XING - 2018 Eagerly evaluate task Register callbacks Similar to Future#onComplete @yifan_xing_e
  • 26. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: Eager/ Lazy YIFAN XING - 2018 Evaluate eagerly Equivalent to Future#successful Defer eager evaluation Similar to Task#eval @yifan_xing_e
  • 27. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: Memoization YIFAN XING - 2018 Evaluated lazily Not memoized, recompute each time the Task is executed. Evaluate lazily Evaluated exactly ONCE Result is memoized @yifan_xing_e
  • 28. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: MemoizeOnSuccess YIFAN XING - 2018 Exceptions are not cached. Only cache the first successful result @yifan_xing_e
  • 29. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: Alternative Thread Pool YIFAN XING - 2018 Overrides default scheduler Overriding the "default" scheduler can only happen ONCE Task is immutable @yifan_xing_e
  • 30. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Monix: CancelableFuture YIFAN XING - 2018 Calling it multiple times has the same side- effect as calling it only once. @yifan_xing_e
  • 31. - A value - Executed when constructed - Eager evaluation - ExecutionContext needed whenever use       operators A function -  Execution controllable by user -  Lazy evaluation -  ExecutionScheduler needed whenever -   use runAsync    MonixFuture Eager/Lazy A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 32. - Memoized by default - Evaluated once - Store result Explicitly use memoization operators -  Evaluated whenever needed -  Referential transparency -  MonixFuture Memoization A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 33. - Sometimes errors are                             not propagated Scheduler.reportFailure -  Defaults to System.err -  Allow customized logging tools -  MonixFuture ErrorHandling A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 34. Future Monix A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 35. FutureMonix A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 36. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Debugging BEST PRACTICES In Asynchronous Programs @yifan_xing_e
  • 37. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
  • 38. Writing Async Code 1. Rethink before adding more code 2. Adding more code to fix problems will usually add more bugs D e b u g 1. Test code in isolation 2. Observe what is happening 3. Confirm behavior before moving on T e s t
  • 39. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Using Await & Print YIFAN XING - 2018 @yifan_xing_e TimeoutException if after waiting for the specified time awaitable is still not ready atMost may be: A finite positive duration Negative (no waiting is done) Duration.Inf for unbounded waiting
  • 40. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA ScalaTest: ScalaFutures YIFAN XING - 2018 @yifan_xing_e Default timeout 150ms TestFailedException if not finished after timeout
  • 41. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Resources: Async Debugger & Capture YIFAN XING - 2018 @yifan_xing_e IntelliJ Capture (2017): Iulian Dragos - Async Debugger in Scala IDE IntelliJ IDEA 2017.1 EAP Extends Debugger with Async Stacktraces Developer's Explanation Debugging Tutorial Async Stacktraces Stack Retention Scala IDE Doc Demo: Jamie Allen Rethink the Debugger: Scala Days 2014
  • 42. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA Using Async Debugger / Capture YIFAN XING - 2018 @yifan_xing_e Substituting its parts related to the asynchronous code execution with where it was called Sender: Async code executor Receiver: executed code Config exact signatures of methods of the async code Stores the stack and variables info in a map (1000 stacks), where the key is a parameter with the specified number.
  • 43. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA YIFAN XING - 2018 @yifan_xing_e ( Sorry Alex :P )
  • 44. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA YIFAN XING - 2018 @yifan_xing_e
  • 45. T H A N K Y O U @yifan_xing_e