5. Sync & Async
5
Synchronous call Asynchronous call
● Caller WAITS for method to
complete
● Blocking
● Easy to program/understand
● Method returns immediately to
caller and executes callback
● Non-blocking
● Run several methods
concurrently
● Scalability
● Harder to program
15. AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask.html
15
When to use? Trigger Triggered From Runs On Limitations /
Drawbacks
- Relatively long
task (UI thread
blocking) with a
need to
communicate with
main thread.[3]
- For tasks in
parallel use
multiple instances
OR Executor [1]
Call to method
execute()
Main Thread Worker thread.
However, Main
thread methods
may be invoked in
between to
publish progress.
- one instance can
only be executed
once (hence
cannot run in a
loop) [2]
- Must be created
and executed from
the Main thread
17. RxAndroidhttp://reactivex.io/
17
ReactiveX is:
• A combination of the best ideas from
– The Observer pattern
– The Iterator pattern
– Functional programming
• An API for asynchronous programming with observable streams.
22. RxAndroidhttps://nhancv.github.io/product/rxjava/
22
Scheduler
• The SubscribeOn operator designates which thread the Observable will
begin operating on.
• The ObserveOn operator specifies a different Scheduler that the
Observable will use to send notifications to its observers.
Varieties of Scheduler
• Schedulers.computation( )
• Schedulers.io( )
• Schedulers.newThread( )