1 00:00:00,360 --> 00:00:03,420 Whenever you run your application, there are so many moving parts. 2 00:00:03,600 --> 00:00:06,780 The key parts are the CPU, the cores and the thread. 3 00:00:07,530 --> 00:00:11,370 In this lesson, you will learn to tell the difference between CPU, core and thread. 4 00:00:16,070 --> 00:00:21,710 Now, a CPU is often referred to as a processor, and most computers only have one CPU chip. 5 00:00:25,300 --> 00:00:27,490 And so why is a CPU called the processor? 6 00:00:29,040 --> 00:00:33,570 That's because it uses processing units called course to execute instructions. 7 00:00:37,730 --> 00:00:39,680 The following is a four core CPU. 8 00:00:43,930 --> 00:00:45,760 Think of every corner as a worker. 9 00:00:53,280 --> 00:00:56,100 Whenever we write code, we're writing instructions. 10 00:00:56,520 --> 00:00:59,670 And when we were in our application, we're running our code on a thread. 11 00:01:00,210 --> 00:01:06,030 This thread contains the instructions that we programmed and all of the instructions that we programmed 12 00:01:06,030 --> 00:01:06,810 on this thread. 13 00:01:06,900 --> 00:01:08,670 They're sent over to the processor. 14 00:01:08,760 --> 00:01:14,160 We're sending these instructions to the processor and it's the cause the workers inside the processor 15 00:01:14,160 --> 00:01:18,330 that execute tasks based on the instructions that the thread provides. 16 00:01:21,570 --> 00:01:26,340 And it follows that the more cores you have, the more instructions that can be processed at the same 17 00:01:26,340 --> 00:01:26,670 time. 18 00:01:42,200 --> 00:01:43,400 Here's an exercise for you. 19 00:01:43,730 --> 00:01:47,870 Go on Google and look up how to check the number of CPU cores inside your machine. 20 00:01:48,350 --> 00:01:49,930 We'll give you some time to parse the video. 21 00:01:55,250 --> 00:01:55,640 OK. 22 00:01:55,670 --> 00:02:00,470 As of this recording, I'm using a MacBook Air M1, which has an eight core CPU. 23 00:02:04,720 --> 00:02:08,650 OK, now what is a thread, a thread is a path of execution. 24 00:02:09,100 --> 00:02:11,740 The instructions in your app run on a thread. 25 00:02:15,700 --> 00:02:20,410 Going back to our diagram, each thread acts as a pathway for instructions to flow through. 26 00:02:25,530 --> 00:02:26,980 Now, this is really important. 27 00:02:27,030 --> 00:02:31,560 So far, every application that we built in this course has been single threaded. 28 00:02:32,250 --> 00:02:33,870 I want to repeat this one more time. 29 00:02:34,260 --> 00:02:38,220 Every application that we built in this course has been single threaded. 30 00:02:44,010 --> 00:02:48,840 So every time we ran our single threaded application, the instructions were being run on a single thread. 31 00:02:49,350 --> 00:02:55,230 Our thread is like a conveyor belt of instructions and eventually every instruction is processed by 32 00:02:55,230 --> 00:02:56,790 the core and our core. 33 00:02:56,790 --> 00:03:01,050 Our worker processes tasks based on the instructions that it receives. 34 00:03:05,540 --> 00:03:08,870 Now, a multi-threaded application runs on many threads. 35 00:03:14,230 --> 00:03:19,270 When you run a multi-threaded application, different parts of the code are running on different threads. 36 00:03:20,080 --> 00:03:24,700 This can be more efficient because different parts of your code are being processed at the same time. 37 00:03:25,820 --> 00:03:30,590 And so once again, each threat is like a conveyor belt of instructions, and eventually every instruction 38 00:03:30,590 --> 00:03:35,270 gets processed by a court which performs tasks based on the instructions that it receives. 39 00:03:35,510 --> 00:03:40,340 And once every instruction has been processed, you in essence, run your application. 40 00:03:43,640 --> 00:03:44,960 Let's talk about the main thread. 41 00:03:45,200 --> 00:03:47,810 By default, your application is single threaded. 42 00:03:50,380 --> 00:03:53,200 The thread your code runs on is known as the main thread. 43 00:03:55,350 --> 00:03:58,740 If I run the following code, it's going to run it on the main thread. 44 00:03:59,190 --> 00:04:04,260 In other words, the series of instructions we programmed execute on the main thread, and the main 45 00:04:04,260 --> 00:04:10,020 thread is like a conveyor belt that passes every instructions to a single core and the core inside of 46 00:04:10,020 --> 00:04:10,670 your computer. 47 00:04:10,680 --> 00:04:16,170 It performs tasks based on the instructions that it receives from your main thread, thereby running 48 00:04:16,170 --> 00:04:17,100 your application. 49 00:04:21,180 --> 00:04:23,100 OK, let's recap what we learned so far. 50 00:04:23,130 --> 00:04:29,550 A CPU chip has multiple cores in each core is a worker that can perform tasks based on the instructions 51 00:04:29,550 --> 00:04:30,390 that it receives. 52 00:04:31,080 --> 00:04:35,430 Each thread is a conveyor belt of instructions that a core eventually processes.