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