Loading video player…

What Is a Thread?

In this lesson, you’ll cover some terminology:

  • CPU (central processing unit) is a piece of hardware in a computer that executes binary code.
  • OS (operating system) is software that schedules when programs can use the CPU.
  • Process is a program that is being executed.
  • Thread is part of a process.

Blocking happens when a thread is stuck, waiting for a something to finish so it can complete its function. When single-threaded apps get blocked, this causes a poor user experience and slower overall execution time.

Multi-threaded apps can execute more than one function at what appears to be the same time. While one thread is blocked, other threads can continue their execution.

00:00 In this first lesson, let’s just talk about what a thread is. Before we talk about threads, there are a couple of terms that we should probably understand. The CPU, or the central processing unit, is a piece of hardware that does the execution of your code. When you write a Python program, it eventually gets translated into binary code, or 1s and 0s, that gets ran through the CPU, and the CPU translates that code into instructions that manipulate other parts of the computer. Maybe it’s saving something to the hard drive, maybe it’s printing a message to your screen.

00:39 These instructions are executed by the CPU. You might ask, “What actually runs your code through the CPU?” Well, that is one of the functions of an operating system, or an OS. The OS handles the scheduling of when the CPU can be used.

00:57 The CPU is in very high demand. Every program needs to use it to perform its function, so the operating system handles when and what programs can use the CPU.

01:08 A process is a program that is currently using the CPU.

01:15 A thread is simply part of a process. It’s part of a program that is using the CPU. Some programs are very simple and they only really do one thing, so they only need one thread, whereas other more complex programs require a lot of different functionality and efficiency, and they might use many threads.

01:39 Here we have a squiggly little line that represents a thread and a gray rectangular shape that represents some barrier or blockage that is stopping our program from completing. It could be many things.

01:54 It could be waiting for a user’s input, it could be some heavy computational effort like processing a video file or something like that—something that takes time and prevents us from completing what we want to do.

02:10 In a single-threaded application, if we have a barrier like this, our code is going to execute and it’s going to hit that barrier and just have to wait until something finishes so it can move on and end the program. In a multi-threaded application, an application with many threads, one of our threads can execute and hit the barrier—maybe it’s waiting for a user to enter its password or something like that while other functionality, other parts of our program can go on and continue executing, continue using the CPU while the first thread is blocked and waiting for user input, in this case. And another thread can go on and do its thing.

02:54 You can probably see how introducing multiple threads into your program can make them more efficient and have a shorter overall execution time.

Avatar image for Colin Potter

Colin Potter on July 14, 2020

Lee Gaines, there are short and curlies all over my screen :)

Become a Member to join the conversation.