Extending the Thread Class6:10 with Ben Deitch
In this video we’ll see a new way to run code on a Thread, and start downloading a playlist instead of just one song!
Using a runnable with a thread is an easy way to route some of your app's 0:00 heavier traffic off of the main thread, but it's not the only way. 0:04 There's actually two ways we can run code on a separate thread. 0:09 We can pass in a runnable to a new thread object, which we're currently doing, 0:13 or we can extend the thread class and override its run method. 0:18 Let's refactor our code to instead use this second method 0:23 of extending the thread class. 0:26 Let's start by creating a new class called DownloadThread. 0:29 And let's make it extend the thread class. 0:38 Then let's override the run method of our new DownloadThread class by 0:42 hitting Ctrl+O to bring up the override dialog and then selecting the run method. 0:47 Then instead of calling super.run, let's call our downloadSong method instead. 0:55 Let's cut the method out of MainActivity, And 1:02 paste it into the bottom of our DownloadThread class. 1:09 Then let's use Alt+Enter to create a new tag constant, 1:16 just like we did in MainActivity. 1:20 .class.getSimpleName. 1:25 Next, let's call downloadSong instead of super.run. 1:30 Lastly, back in MainActivity and our download button's onClick listener, 1:37 let's get rid of our runnable object, 1:42 And use our new DownloadThread class instead of the normal thread class. 1:49 Looks good. 1:59 Let's quickly test our app to make sure it still works the same. 2:01 And success. 2:08 It works just like it did before. 2:12 This code is exactly the same as when we were using a runnable and 2:15 passing it to a new thread. 2:19 All right, let's take a minute to see what's going on behind the scenes. 2:22 Let's start by opening the Android device monitor by clicking up here. 2:27 On the left, we see a list of running processes, 2:37 and if we expand this list, look at that, 2:42 there's our process right down here. 2:46 Let's click on it and 2:51 then click this button to show all the threads in our app's process. 2:53 Then if we go back to our app and 2:59 we click on the Download button, there's our thread. 3:03 And if we click it a bunch, a bunch of threads. 3:07 If you're ever curious about what's going on inside your device, 3:14 the Android device monitor is a great place to start looking. 3:17 Now instead of downloading a single song, let's say we want to download a playlist. 3:22 In fact, there's an array of songs in the teacher's notes for us to use. 3:28 Let's go back to the code and create a new class called Playlist. 3:33 Then, let's copy and paste the songs array into our new class. 3:48 Making the songs array public static means we'll be able to access it from 3:55 anywhere in our app. 3:59 Let's close this file and head over to our DownloadThread class. 4:03 Let's also minimize the log. 4:09 The last thing we need to do is call the downloadSong method for 4:13 each song in our playlist. 4:17 Sounds like a good place for a for each loop. 4:19 Inside the run method, lets type for 4:23 (String song: Playlist.songs) and 4:27 then let's add our curly brackets around the download song method. 4:31 Before we test this, let's take a minute to talk about what would happen if we 4:41 instead added our loop around the code and our download button's onClick method. 4:46 If we looped here, we would create a new thread for 4:55 each song, and all the songs would be downloading at the same time. 4:59 You'd think this would be great, but by trying to download all the songs at once, 5:04 we'd be giving our users a bad experience. 5:10 They want to listen to their playlist as soon as possible, and we've only got so 5:13 much bandwidth we can use for downloads. 5:18 So if we're spreading that bandwidth out over the whole playlist, 5:21 they'll have to wait a long time before they can listen to any song. 5:25 It would be better if we downloaded the songs one at a time. 5:30 This way, our users can start enjoying their music a lot quicker. 5:35 All right, now let's test our app and see what happens. 5:40 I'll click the download button and in ten seconds, we can start 5:48 to see our songs finish downloading one after the other, just like we wanted. 5:53 We've got our playlist and we're downloading the songs one at a time, but 5:59 there's still more work to be done. 6:03 In the next video, we'll learn about some potential errors and how to fix them. 6:05
You need to sign up for Treehouse in order to download course files.Sign up