18.ISIS3510 Multi Threading
18.ISIS3510 Multi Threading
18.ISIS3510 Multi Threading
3.0
MULTI
THREADING
Why should we use multi-
threading in mobile
apps?
Mobile apps are prone to
performance bugs….
Mobile apps are prone to
performance bugs….
because of hardware
constraints and OS
GUI lagging and
Application Not
Responding Errors (ANRs)
PERFORMANCE BUGS
IN MOBILE APPS
Memory bloats and Out of
Memory Exceptions
(OOMs)
Android-related
Performance Issues
96 64 6 47 4 33
Missing
Unneeded Unused condition
Unneeded Skippable
object Unneeded imports/ check before
activity function
instantiati variable dependen executing
instances operation
on cies 1 6
2 1 2 1
120 52 4 32 42
Lagging Lagging
Reference Suboptimal Missed Missed Multithrea Inefficien. Objects Complex
Unreleased Unneded Costly String Compiler when because
cycle algorithmic caching batching ding- data instantiat. graphical
objects computat. operation operations settings scrolling of data
related complexity opportunity opportunity related represent. in loop effects
lists loading
53 15 6 1 2 7 4 2 12 5 1 1 9 2 2
Missing Missing
check Date- Missed
use of Unreleased Strong Unneeded Threading
condition formattin multithr.
auto pointers reference before threads optimizat.
g opportunity
release executing
39 14 9 2 1 1 2 2
GUI lagging and
ANRs
App process
Operatin
g Main / UI
system Thread
GUI lagging and
ANRs App process
Operatin
g Main / UI
system Thread
Computationally
expensive
operation
GUI lagging and
ANRs OS App process Main
Computationally
expensive
operation
GUI lagging and
ANRs OS App process Main
Response to UI is updated
First event
WTn
Main
WT2
WT1
Async execution with Futures: futures are
executed when the main thread is free
Main
Future Future is
is executed on
created main thread
CONCURRENT
Work submitted to dispatch
queues executes on a pool of
threads managed by the system
SYNCHRONOUS The caller waits until the task
EXECUTION (SYNC) finishes
DispatchQueue.main
DispatchQueue.global(qos: .userInteractive)
DispatchQueue.global(qos: .userInitiated)
DispatchQueue.global(qos: .utility)
DispatchQueue.global(qos: .background)
DispatchQueue.global(qos: .unspecified)
DISPATCH QUEUES
Main thread,
DispatchQueue.main
serial
DispatchQueue.global(qos: .userInteractive)
DispatchQueue.global(qos: .userInitiated) Background,
DispatchQueue.global(qos: .utility) concurrent,
DispatchQueue.global(qos: .background) shared across
DispatchQueue.global(qos: .unspecified)
all apps
DispatchQueue.main.async {
// update ui here
}
IOS
DispatchQueue.global(qos: .background).async {
// do your job here
}
let main = DispatchQueue.main
IOS
DispatchQueue.main.async {
// update ui here
}
}
let delay = DispatchTime.now() + .seconds(120)
IOS
DispatchQueue.main.asyncAfter(deadline: delay) {
// Do your stuff
}
let queue = DispatchQueue(label: “serial.example”)
queue.sync {
// ...
}
IOS
DispatchQueue(label: “concurrent.example",
attributes: .concurrent).sync {
// . . .
}
How does multi-threading
work in Android (Java)?
Several options are available in Java for
multi-threading, which is both pro and cons at
the same time
https://developer.android.com/reference/android/os/AsyncTask.html
ANDROID
https://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask
argument
ANDROID
https://developer.android.com/reference/android/os/AsyncTask.html
ANDROID
https://developer.android.com/reference/android/os/AsyncTask.html
ANDROID
https://developer.android.com/reference/android/os/AsyncTask.html
ANDROID
Progress
value
https://developer.android.com/reference/android/os/AsyncTask.html
ANDROID
Result
value
https://developer.android.com/reference/android/os/AsyncTask.html
How does multi-threading
work in Kotlin?
Kotlin combines the multi-threading options
available in Android, with coroutines that
allows for suspending functions
executes asynchronously:
https://developer.android.com/kotlin/coroutines
ANDROID Coroutines - Long Running task
0.4 seconds
KOTLIN
Main
Network
request on
main thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
ANDROID Coroutines - Long Running task
0.4 seconds
KOTLIN
Main
Network
request on
main thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
ANDROID Coroutines - Long Running task
0.4 seconds
KOTLIN
Main
Network
request on
main thread
show(result)
}
}
}
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
HTTP Request (off the main thread)
class ViewModel: ViewModel() {
fun fetchDocs() {
get("developer.android.com") { result ->
KOTLIN
show(result)
}
}
}
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
show(result)
}
•suspend — pause the execution of the current coroutine, saving all local variables
•resume — continue a suspended coroutine from the place it was paused
You can only suspend functions called by other suspend functions, or using a coroutine
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Long Running task
Callback Coroutine
KOTLIN
Main
Thread
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Coroutines - Main Safety
Coroutines will run on the main thread by default,
and suspend does not mean background.
KOTLIN
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
KOTLIN Coroutines - Main Safety
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
KOTLIN Coroutines - Main Safety
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
KOTLIN Coroutines - Main Safety
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
How to run a
Coroutine?
Coroutines
You can start coroutines in one of two ways:
launch
async
Starts a new coroutine and
Starts a new coroutine and allows
KOTLIN
https://developer.android.com/kotlin/coroutines
launch is a bridge from regular functions into
coroutines
Coroutines
You can start coroutines in one of two ways:
launch
async
Starts a new coroutine and
Starts a new coroutine and allows
KOTLIN
https://developer.android.com/kotlin/coroutines
Coroutines
You can start coroutines in one of two ways:
launch
async
Starts a new coroutine and
Starts a new coroutine and allows
KOTLIN
https://developer.android.com/kotlin/coroutines
Coroutines scopes
model is active
You can
modify UI after
doing a
background
work without
doing to many
steps
https://developer.android.com/kotlin/coroutines
Coroutines
Possible Problems:
KOTLIN
-Leaked Work
-Problems handling
errors
https://developer.android.com/kotlin/coroutines
Coroutines
Possible Problems:
KOTLIN
-Leaked Work.
-Problems handling
errors
https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd
Coroutines
Theory:
https://developer.android.com/kotlin/coroutines
https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd
Practical Examples:
https://medium.com/androiddevelopers/coroutines-on-android-part-iii-real-work-2ba8a2ec2f45
And what about
flutter?
Flutter is single-threaded, therefore, multi-
threading is not available. Therefore, to avoid
blocking the main thread, async functions
should be used.
The code to
be executed
when the
value is
returned
What about the
async/await way for
futures?
Futures
Let’s go back to a previous example
FLUTTER
Futures
To make it work (blocking the main thread) we need to use async/
await
FLUTTER
FLUTTER Futures