Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Asynchronous processing allows us to perform work in the background while letting users continue interacting with the user interface of our app. It's very common for Android, especially when interacting with the web.
[MUSIC]
0:00
When we last saw stormy,
it was just that pretty storming and
0:04
not working due to a network
on main thread exceptionary.
0:08
As the name would indicate,
we were getting the error because our
0:13
app was trying to get data
while on the main thread.
0:16
So what exactly is a thread?
0:19
We're supposed to be doing
entry development, not sewing.
0:22
Let's talk about this concept of threads,
as it relates to apps and
0:25
computer programs in general.
0:29
We've all experienced this at
some point with our phones and
0:31
it can cause lots of frustrations.
0:34
You do something in an app,
and the app lags.
0:37
You tap somewhere else on the screen and
nothing happens.
0:40
Tap, tap, tap and no response.
0:43
As good responsible developers, we want
to make our users happy with our apps.
0:47
One way to accomplish this is to ensure
responsiveness to input from the user.
0:52
We never want to leave
it in a frozen state.
0:57
To accomplish this, we perform work on
separate threads of execution in parallel.
1:00
Let us take a look at an example.
1:06
Let us think about a conversation
we are having with a friend.
1:08
Generally, we want to be
responsive to their input, right?
1:12
No one enjoys having
a one way conversation.
1:15
If I'm talking to Ben I don't want to
roughly leave the conversation to go get
1:18
water, that will leave him waiting and
frustrated.
1:22
Typically, I'd wait to
finish the conversation and
1:26
then get my work in water.
1:28
You know, do two things in order.
1:30
This is know as performing
synchronous tasks.
1:32
What if I'm really thirsty though?
1:35
I could ask someone else to go get me
some water while I'm talking with Ben.
1:38
That work, getting water,
is done in parallel with my main work,
1:42
the conversation with Ben.
1:46
On an Android device, and most computers,
1:49
all the work is typically done
on one main thread of execution.
1:52
My conversation with Ben is like
work being done on the main thread.
1:57
We can perform additional work in
the background like getting water
2:01
by executing it on a separate
asynchronous thread of execution.
2:05
This background thread,
Craig here in this example
2:10
can perform long running operations that
would otherwise clog up the main thread.
2:14
What do I mean by a thread of execution?
2:19
In programming, we use the term thread to
refer to a path of execution for code.
2:22
We can make our code perform
multiple tasks at once,
2:28
just like the conversation and
water example we just explored.
2:31
This is known as concurrency which means
2:35
having multiple things
happen at the same time.
2:38
How does this relate
to Android programing?
2:41
Android has many ways to handle
asynchronous processing.
2:44
Further, it's built right into okay HTTP.
2:48
We'll explore that more here shortly.
2:53
Why are we even talking about this, well,
just like our conversation and water
2:55
example, we want our app to be completely
attentive and responsive to our user.
3:00
You can think if the Android operating
system as a highway with multiple lanes.
3:06
Each lane is a thread used for
processing code.
3:11
On this highway,
we have vehicles speeding along.
3:15
Think of these vehicles as code
executing in different apps or
3:18
different parts of the system.
3:21
The code we just wrote
is synchronous code.
3:23
This is like blocking the lane
with a slow moving truck.
3:26
When we write asynchronous code,
we move this truck to another lane, and
3:30
it keeps the traffic on the main
lane flowing as normal.
3:34
Let's think about
networking on this highway.
3:38
We all know that the network can
be a little unreliable at times.
3:41
When we make a network call on our app
what happens if we have issues and
3:46
our request takes a long time?
3:50
We're making that all on our main thread
like we are in our code right now.
3:52
It can block the user interface.
3:56
None of the user interface
code will get executed and
3:59
the user won't be able to
do anything with the app.
4:01
They would think it is unresponsive or
broken, leading to frustration and
4:05
then they might even, delete our app.
4:09
All of our networking code should
be done in a background thread.
4:13
One of these side lanes.
4:16
This way, we won't block
the UI from being responsive.
4:18
When this network request completes,
we need to move back
4:22
from the background thread to
the main thread to update our UI,
4:25
since all of UI updates
are done on the main thread.
4:29
That's a lot to take in, I know, but
concurrency can improve the responsiveness
4:33
of our code by ensuring that our main
thread is free to respond to user events.
4:39
Our next step, then,
is to change our code to be asynchronous.
4:44
Let's see how we can do that, next.
4:48
You need to sign up for Treehouse in order to download course files.
Sign up