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
In this video we’ll see how we can communicate with our Thread by using several new objects!
-
0:00
We've just finished adding a playlist to our app, and
-
0:03
we're even making sure to download one song at a time.
-
0:06
What else could we want?
-
0:08
Well, unfortunately we really shouldn't be using the playlist
-
0:13
inside our download thread.
-
0:16
If this were a larger app that had more than one developer, it wouldn't be too
-
0:20
hard to imagine someone else modifying our playlist outside of our download thread.
-
0:25
And if our playlist changes for any reason,
-
0:28
all of a sudden we'd be downloading the wrong songs.
-
0:32
What we need is a queue for the songs we want to download.
-
0:36
Once one song finishes, the next song in the queue would start downloading.
-
0:41
We can implement this by adding something called a message queue to our thread.
-
0:46
And in order to use a message queue,
-
0:49
we'll also need to be aware of handlers, loopers, and messages.
-
0:53
It's a lot to cover, but the following examples should help elucidate
-
0:57
the role each of these objects plays in the bigger picture.
-
1:01
Imagine a fully stocked burrito truck.
-
1:03
This burrito truck represents our thread.
-
1:06
Just like a thread is a place where we can execute our code,
-
1:10
the burrito truck is a place where we can execute making burritos.
-
1:14
In this example a runnable would be a recipe.
-
1:18
Something like, take one tortilla add a quarter pound of shredded chicken and
-
1:23
a half cup of shredded cheese, mix and fold into the shape of a burrito.
-
1:28
So before, when we were just using a runnable and a thread,
-
1:32
that would be like if we just showed up at the burrito truck with our own recipe and
-
1:37
then we just went inside and made the burrito ourselves.
-
1:41
But now we'd like our thread, burrito truck,
-
1:44
to have a queue of what it should be working on.
-
1:47
The first change we're going to make,
-
1:49
is we're going to hire someone to work in the burrito truck.
-
1:52
We'll call him Tim, and he represents the handler object.
-
1:57
In Android each handler is associated with only one thread.
-
2:01
And that handler is responsible for sending and
-
2:05
processing messages and runnables for the thread.
-
2:09
So Tim, our handler, only works at this burrito truck, our thread.
-
2:15
And he's responsible for taking and preparing the orders,
-
2:19
messages and runnables.
-
2:22
We're already familiar with runnables, but what's a message?
-
2:26
A message is just a way for us to send some arbitrary data to our handler.
-
2:31
It's then up to the handler how it will handle the message.
-
2:36
If we wanted to order a burrito, we could give Tim a recipe, or
-
2:40
we could order off the menu.
-
2:41
One breakfast burrito please.
-
2:44
When we order off the menu, that's an example of using a message.
-
2:48
We don't need to know how to make a breakfast burrito
-
2:51
because Tim knows how to handle it.
-
2:54
Once Tim takes our order, he's going to put it at the end of the list of orders.
-
2:59
This is the message queue.
-
3:00
It contains all the runnables and messages that our thread still needs to handle.
-
3:06
Now Tim's daughter Looper is too young to help with most of the burrito truck work.
-
3:10
But she's still eager to help out.
-
3:13
So when Looper notices her dad is about to finish an order,
-
3:17
she grabs the next order from the message queue and gives it to her dad.
-
3:21
Way to help out Looper.
-
3:24
All right, let's walk through an example to see how all these pieces work together.
-
3:29
A man walks up to the burrito truck with the recipe for a burrito he'd like.
-
3:34
Tim takes the order and adds it to the message queue.
-
3:37
Then Tim gets back to working on a previous order.
-
3:40
When Tim finishes the previous order, Looper grabs the next
-
3:44
order from the message queue and Tim starts working on this new order.
-
3:49
Then another guy walks up and places an order for a breakfast burrito.
-
3:53
Tim takes the order and adds it to the message queue.
-
3:56
Then Tim gets back to working on a previous order.
-
3:59
When Tim finishes the previous order Looper grabs the next order from
-
4:04
the message queue and Tim starts working on this new order.
-
4:08
Looks like Tim's an awfully busy guy.
-
4:11
Let's take a short break and then we'll see how to implement this in code.
You need to sign up for Treehouse in order to download course files.
Sign up