Accomplishing more than one task at-a-time is called multi-tasking. There are two types; process-based and thread-based (multithreading). Below, you will find the characteristics of these types.
Process-based multitasking
- In Process-based multitasking, several tasks execute simultaneously – where each task acts as a separate independent program (process).
- Suppose you have opened a browser to write some text in the text editor. And we are listening to an mp3 song and downloading an application from the browser via the internet. Also, you have opened four desktop process applications viz VSCode Editor, Calculator, Notepad, and Google Chrome. These work independently of each other.
Thread based multitasking
- In thread-based multitasking, several tasks execute simultaneously. You can say each one is a thread.
- Suppose there is a code of 5000 lines consisting of applications related to 2 customers. Both customer applications are independent of each other. Each customer application is of each 2500 lines of code.
- Now, say the customer1 application takes 2 min, and the customer2 application takes 3 mins to complete the program on executing the code line by line.
- Now, there is a program of 5000 lines of code available, so if each line executes from top to bottom, the program code execution time will take a minimum of 5 mins.
- But, in the same program, there is no relation between the first 2500 lines of code and the rest 2500 lines of code as both are related to different customers who are independent.
- So if there’s no dependency, why the customer 2 code will wait for the customer 1 code to complete? It is better to execute both customer code applications simultaneously.
Whenever we write any python code and there is no thread explicitly created, then by default there is one thread called main thread. So, it is the responsibility of the main thread to execute the code.
If both the code will be executed simultaneously, then within less time we will be able to complete the task. This type of multitasking is called thread-based multitasking and each independent part of the program is called a thread.
How to Write Thread Logic in Python
Here is the helpful logic to write thread concept.
import threading
x = int(’12’)
print(x)
print(“The current executing thread name is: ”,threading.current_thread().getName())
Output
12
The current executing thread name is: MainThread
Terms used in multi-threading
Process: computer program instance which is executed which mainly has 3 basic components:
- A program to be executed.
- The data needed by the program (variables, buffers, etc.).
- Program execution context (process state).
Thread: An entity within a process or a python object which is the smallest processing unit that can be performed in an operating system and is a separate execution flow. In other words, it is a subset of processes. Where thread store information? A thread contains information in a Thread Control Block(TCB).
- Thread ID: A thread Identifier is a unique ID that is assigned to every thread.
- Stack Pointer: It points to the thread’s stack in the process and contains the local variables which are under thread scope.
- Program Counter: It is a register that stores the instruction address currently being executed by the thread.
- Thread State: It refers to the state of the thread which can be ready, running, waiting, start/or it’s done
- Thread’s Register Set: These are the registers that are assigned to the thread for computations.
- Parent Process Pointer: It is a pointer to the Process control block of the process that the thread lives on.
Related