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