Threading in Python(Part-1)

Preet Padariya
6 min readApr 11, 2022

--

What is Thread?

All programmers are familiar with writing a procedural program which refers to sequential execution of a program. A program can be described as the set of statements which we don’t need to dig up that in deep. A normal program which you have written till now, most probably they all have one start, one end point and execution sequence; unless you don’t have knowledge of threads. This simply means that your program has single life, single point of execution at any given time during execution.

A single thread is similar to sequential program described as earlier. A single thread is also having single point of execution and one start point and one end point. It is stated that at any given time during runtime of a thread, there exist only a single point of execution, which is always true.

Definition: A thread is a single sequential flow of control within a program.

A thread is a separate flow of execution. The real excitement surrounding threads is not about a single sequential thread. Rather, it’s about the use of multiple threads running at the same time and performing different tasks in a single program. This use is illustrated in the next figure.

Single Threading (Reference)
Multiple Threading (Reference)

For instance, a Web browser is an example of a multithreaded application. Within a typical browser, you can scroll a page while it’s downloading an applet or an image, play animation and sound concurrently, print a page in the background while you download a new page, or watch three sorting algorithms race to the finish.

Also, any kind of game is also an example of multi-threaded application because playing sound, showing graphics etc. different kind of works are being managed by multiple threads in one program to make games even smoother for players to play and enjoy.

A state of thread is a term which determines the fate of thread

A particular state of thread will decide that how this thread will proceed further. At any point of time, a thread can exist in following states.

· New

· Runnable

· Blocked

· Waiting

· Timed Waiting

· Terminated

flow of thread(reference)

Life Cycle of a thread

1. New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when the thread is in this state. When a thread lies in the new state, its code is yet to be run and hasn’t started to execute.

2. Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread. Each and every thread runs for a short while and then pauses and relinquishes the CPU to another thread so that other threads can get a chance to run. When this happens, all such threads that are ready to run, waiting for the CPU and the currently running thread lie in a runnable state.

3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in one of the following states:

1. Blocked

2. Waiting

4. Timed Waiting: A thread lies in a timed waiting state when it calls a method with a time-out parameter. A thread lies in this state until the timeout is completed or until a notification is received. For example, when a thread calls sleep or a conditional wait, it is moved to a timed waiting state.

5. Terminated State: A thread terminates because of either of the following reasons:

1. Because it exits normally. This happens when the code of the thread has been entirely executed by the program.

2. Because there occurred some unusual erroneous event, like segmentation fault or an unhandled exception.

“Multi-tasking”, leading towards “multi-threading”

Let us just first understand the two terminologies “multi-tasking” and “multi-threading” over here.

“Multi-tasking” is concurrent performance/progress of several task which leads to performance of multiple tasks at a same time.

As we have discussed earlier that the beauty of thread is laying in multiple threads at a time and so, executing multiple tasks simultaneously is the concept of “multi-threading”. Since we are talking about executing multiple tasks simultaneously, we might face some of the difficulties while doing or trying to achieve “multi-threading” environment such as;

· First and for most, we need to make sure that concurrency must be achieved and somehow, we need to maintain context-switching.

· Writing and debugging the code which is made to achieve multi-threading, is difficult.

Issues with Multithreading Programs(references)

However, we can deal with those issues. First of all, accept those issues as truth and let us first understand the types of multi-threading.

Multi-threading Environment

The basic overview has been already given to you in above section. In this section we will see that what are the types of multi-threading which we can achieve.

Process based multi-threading

Executing several tasks simultaneously where each task is having separate independent program is called Process based multi-Threading.

Ex: While typing a program in the editor we can listen audio songs from the same system. Yet same time we can also download a file from internet.

All these tasks will be executed simultaneously and independent of each other hence it is process based multi-Threading. Process based multi-Threading is the best suitable at OS level.

Thread based multi-threading

Executing several tasks simultaneously where each task is separate independent part of the same program is called Thread based multi-Threading, and each independent part is called Thread.

Thread based multi-Threading is best suitable at programmatic level.

Whether it is process based or thread based the main objective of the multi-Threading is to reduce response time of the system and to improve performance. The main important application areas of multi-Threading are

1. To develop Multimedia Graphics

2. To develop animations

3. To develop video games

4. To develop web servers and application server etc.

In this whole section, we have briefed you about the thread. Now, you will be able to differentiate between thread, threading, multi-threading, multi-tasking, etc. Here are some key-words for you to summarise this whole section. From next section we will deep dive into programming.

Useful terms to remember

· Thread

· Life cycle of a thread

· Thread states

· Multi-tasking

· Multi-threading

· Process based multi-threading

· Thread based multi-threading

Conclusion of this part

We have learnt the basic concepts of thread and what is the basic definition and states of thread. The states and life cycle of a thread. We have also learnt that what is multi-tasking and multi-threading. Why we need multi-threading! What are the problems while we want to achieve multi-threading! What are the types of multi-threading and all that stuff? Now, you will be able to understand the upcoming programming-based implementation of the threading.

--

--

Responses (1)