1 00:00:00,008 --> 00:00:04,947 [MUSIC] 2 00:00:04,947 --> 00:00:08,649 When we last saw stormy, it was just that pretty storming and 3 00:00:08,649 --> 00:00:13,040 not working due to a network on main thread exceptionary. 4 00:00:13,040 --> 00:00:16,020 As the name would indicate, we were getting the error because our 5 00:00:16,020 --> 00:00:18,880 app was trying to get data while on the main thread. 6 00:00:19,990 --> 00:00:22,130 So what exactly is a thread? 7 00:00:22,130 --> 00:00:24,710 We're supposed to be doing entry development, not sewing. 8 00:00:25,840 --> 00:00:29,010 Let's talk about this concept of threads, as it relates to apps and 9 00:00:29,010 --> 00:00:30,180 computer programs in general. 10 00:00:31,570 --> 00:00:34,510 We've all experienced this at some point with our phones and 11 00:00:34,510 --> 00:00:36,580 it can cause lots of frustrations. 12 00:00:37,580 --> 00:00:40,640 You do something in an app, and the app lags. 13 00:00:40,640 --> 00:00:43,930 You tap somewhere else on the screen and nothing happens. 14 00:00:43,930 --> 00:00:46,230 Tap, tap, tap and no response. 15 00:00:47,300 --> 00:00:52,400 As good responsible developers, we want to make our users happy with our apps. 16 00:00:52,400 --> 00:00:57,750 One way to accomplish this is to ensure responsiveness to input from the user. 17 00:00:57,750 --> 00:01:00,300 We never want to leave it in a frozen state. 18 00:01:00,300 --> 00:01:05,020 To accomplish this, we perform work on separate threads of execution in parallel. 19 00:01:06,150 --> 00:01:07,470 Let us take a look at an example. 20 00:01:08,800 --> 00:01:12,070 Let us think about a conversation we are having with a friend. 21 00:01:12,070 --> 00:01:14,670 Generally, we want to be responsive to their input, right? 22 00:01:15,760 --> 00:01:18,630 No one enjoys having a one way conversation. 23 00:01:18,630 --> 00:01:22,500 If I'm talking to Ben I don't want to roughly leave the conversation to go get 24 00:01:22,500 --> 00:01:26,250 water, that will leave him waiting and frustrated. 25 00:01:26,250 --> 00:01:28,760 Typically, I'd wait to finish the conversation and 26 00:01:28,760 --> 00:01:30,080 then get my work in water. 27 00:01:30,080 --> 00:01:32,450 You know, do two things in order. 28 00:01:32,450 --> 00:01:35,950 This is know as performing synchronous tasks. 29 00:01:35,950 --> 00:01:38,290 What if I'm really thirsty though? 30 00:01:38,290 --> 00:01:42,610 I could ask someone else to go get me some water while I'm talking with Ben. 31 00:01:42,610 --> 00:01:46,940 That work, getting water, is done in parallel with my main work, 32 00:01:46,940 --> 00:01:49,070 the conversation with Ben. 33 00:01:49,070 --> 00:01:52,260 On an Android device, and most computers, 34 00:01:52,260 --> 00:01:57,070 all the work is typically done on one main thread of execution. 35 00:01:57,070 --> 00:02:01,410 My conversation with Ben is like work being done on the main thread. 36 00:02:01,410 --> 00:02:05,650 We can perform additional work in the background like getting water 37 00:02:05,650 --> 00:02:10,790 by executing it on a separate asynchronous thread of execution. 38 00:02:10,790 --> 00:02:14,140 This background thread, Craig here in this example 39 00:02:14,140 --> 00:02:18,410 can perform long running operations that would otherwise clog up the main thread. 40 00:02:19,560 --> 00:02:22,260 What do I mean by a thread of execution? 41 00:02:22,260 --> 00:02:28,040 In programming, we use the term thread to refer to a path of execution for code. 42 00:02:28,040 --> 00:02:31,450 We can make our code perform multiple tasks at once, 43 00:02:31,450 --> 00:02:35,340 just like the conversation and water example we just explored. 44 00:02:35,340 --> 00:02:38,410 This is known as concurrency which means 45 00:02:38,410 --> 00:02:40,620 having multiple things happen at the same time. 46 00:02:41,720 --> 00:02:43,760 How does this relate to Android programing? 47 00:02:44,840 --> 00:02:48,910 Android has many ways to handle asynchronous processing. 48 00:02:48,910 --> 00:02:52,050 Further, it's built right into okay HTTP. 49 00:02:53,110 --> 00:02:55,357 We'll explore that more here shortly. 50 00:02:55,357 --> 00:03:00,939 Why are we even talking about this, well, just like our conversation and water 51 00:03:00,939 --> 00:03:06,627 example, we want our app to be completely attentive and responsive to our user. 52 00:03:06,627 --> 00:03:11,460 You can think if the Android operating system as a highway with multiple lanes. 53 00:03:11,460 --> 00:03:15,017 Each lane is a thread used for processing code. 54 00:03:15,017 --> 00:03:18,250 On this highway, we have vehicles speeding along. 55 00:03:18,250 --> 00:03:21,920 Think of these vehicles as code executing in different apps or 56 00:03:21,920 --> 00:03:23,950 different parts of the system. 57 00:03:23,950 --> 00:03:26,830 The code we just wrote is synchronous code. 58 00:03:26,830 --> 00:03:30,430 This is like blocking the lane with a slow moving truck. 59 00:03:30,430 --> 00:03:34,910 When we write asynchronous code, we move this truck to another lane, and 60 00:03:34,910 --> 00:03:37,779 it keeps the traffic on the main lane flowing as normal. 61 00:03:38,810 --> 00:03:41,620 Let's think about networking on this highway. 62 00:03:41,620 --> 00:03:46,880 We all know that the network can be a little unreliable at times. 63 00:03:46,880 --> 00:03:50,950 When we make a network call on our app what happens if we have issues and 64 00:03:50,950 --> 00:03:52,960 our request takes a long time? 65 00:03:52,960 --> 00:03:56,990 We're making that all on our main thread like we are in our code right now. 66 00:03:56,990 --> 00:03:59,152 It can block the user interface. 67 00:03:59,152 --> 00:04:01,980 None of the user interface code will get executed and 68 00:04:01,980 --> 00:04:05,600 the user won't be able to do anything with the app. 69 00:04:05,600 --> 00:04:09,750 They would think it is unresponsive or broken, leading to frustration and 70 00:04:09,750 --> 00:04:13,680 then they might even, delete our app. 71 00:04:13,680 --> 00:04:16,840 All of our networking code should be done in a background thread. 72 00:04:16,840 --> 00:04:18,670 One of these side lanes. 73 00:04:18,670 --> 00:04:22,510 This way, we won't block the UI from being responsive. 74 00:04:22,510 --> 00:04:25,900 When this network request completes, we need to move back 75 00:04:25,900 --> 00:04:29,160 from the background thread to the main thread to update our UI, 76 00:04:29,160 --> 00:04:32,409 since all of UI updates are done on the main thread. 77 00:04:33,630 --> 00:04:39,120 That's a lot to take in, I know, but concurrency can improve the responsiveness 78 00:04:39,120 --> 00:04:44,590 of our code by ensuring that our main thread is free to respond to user events. 79 00:04:44,590 --> 00:04:48,830 Our next step, then, is to change our code to be asynchronous. 80 00:04:48,830 --> 00:04:50,820 Let's see how we can do that, next.