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 talk about blocking the main Thread, and we see how to use a Runnable with a new Thread!
Related Links
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
All right, let's run the app and
see what happens.
0:00
If we click the Download button and
0:07
then wait ten seconds, one, two, three,
0:11
four, five, six, seven, eight, nine,
0:16
ten, we get a log message But
0:21
we didn't see our toast and
if we click the Download button a bunch.
0:26
It looks like it stops responding.
0:33
And to top it all off,
0:38
we get the dreaded application not
responding dialogue, or A and R..
0:39
That's not good.
0:45
But why is this happening?
0:47
Remember that each Android
app has its own main thread.
0:49
Sometimes called the UI thread.
0:54
And if we do too much
work on the main thread,
0:56
then our UI can become unresponsive.
0:58
And we might see the application
not responding dialogue.
1:02
We can fix this by putting
the burden of our download
1:06
onto a different thread
than the main thread.
1:09
We've seen this before when we
built our weather app, Stormy.
1:13
We saw a highway where each
lane was a different thread.
1:17
And we needed to be careful not to block
the main lane with our downloading code.
1:21
In Stormy, we solved this by using
a method from the OkHttp library that
1:26
handled all of this for us.
1:31
But this time we're going to create and
user own threads.
1:34
Let's start by adding another lane to
our highway by creating a new thread
1:39
object at the bottom
of our OnClick method.
1:43
Let's type Thread thread = newThread and
1:50
notice that there are several constructors
1:55
we can use when creating a thread.
2:00
We'll be using the second
one which takes a runnable.
2:05
A runnable represents a block
of code that can be executed.
2:09
In our highway example,
we can think of the thread as the lane and
2:14
the runnable as the traffic
that flows on that lane.
2:18
Let's add our semi-colon.
2:22
And then add a couple of lines above.
2:25
And create a new runnable object.
2:31
Runnable runnable = new Runnable and
2:34
add a semi-colon.
2:40
Right now our runnable
isn't running anything.
2:46
Let's cut out our download
song method from above.
2:49
And paste it into the run
method of our runnable.
2:54
And now that our runnable represents
a bit more traffic on our highway.
3:01
Let's pass it into our
thread's constructor.
3:05
Awesome, we've put the code that used
to block our UI and a runnable and
3:12
that runnable is also add
up to run on a new thread.
3:17
Now let's give our thread a name
by calling Thread.setname and
3:21
let's name it DownloadThread.
3:29
The last thing we need to do is call
thread.start to start our thread.
3:38
Great, now let's test the app.
3:47
Let's click the Download button.
3:56
And right off the bat, there's our toast.
4:00
And if we click it a bunch more,
no problem.
4:03
Our button stays totally responsive and
4:09
we'll see a log message for
each time it was clicked.
4:12
Great work!
4:18
But before we move on, let's take a minute
to talk about what we learned here.
4:19
We can think of our app as one big
runnable that runs on the main thread.
4:24
If we do a lot of work on this thread,
like a download,
4:29
it can start to slow things down and
the application might become unresponsive.
4:33
To solve this, we create a new runnable
which contains our download code and
4:39
we run it on a new thread.
4:44
This way we can keep our download
from blocking the main thread.
4:46
And more importantly,
we can keep our users happy.
4:51
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