Speed Boost: How Multi-Threading Makes Your Programs Faster

06 / Feb / 2024 by Tushar Saxena 0 comments

Imagine you’re at a grocery store, and you need to buy a bunch of items. Now, you have two options:

Option 1: You’re the only person shopping, and you have to go through each aisle one by one, picking up items and waiting in line at the checkout each time. It’s like a slow-motion shopping marathon, and you end up spending a lot of time.

Option 2: You bring a friend (or a few friends) with you. Each person takes a separate aisle, grabs items simultaneously, and you all meet up at the checkout. It’s like a synchronized shopping dance, and you finish much faster!

Now, replace “you” with a computer program, “aisles” with tasks, and “friends” with threads. That’s essentially multi-threading! In the computer world, programs have different tasks to perform, and multi-threading allows them to tackle those tasks simultaneously, making the whole process faster and more efficient.

In super easy language, what exactly is a thread?

Think of a thread as a helper inside your computer that can do one thing at a time. If your computer is like a kitchen, a thread is like a little chef handling one task, and when you have many chefs (threads), the kitchen gets things done quicker!

The thread has to go through various stages of its life

1. Getting Ready (New): The thread is created but hasn’t started yet.

2. Time to Work (Runnable/Ready): Now the Task is assigned, the thread is all set to start the job.

3. Working Hard (Running/Active): The thread is actively doing its task, just like a worker doing their job.

4. Taking a Break (Waiting/Blocked): Sometimes, the thread needs to wait a bit, like taking a short break until things are ready.

5. Job Done (Dead/Terminiated): Finally, the thread finishes its job and is done for the day, just like a worker going home after work.

How things can be implemented using Python

Python makes things easier with a built-in module threading which takes care of everything.

The thread class is all you need for creating and using multiple threads in your Python program. There are various ways by which we can create threads

Firstly, let’s create a thread by Thread class object

Here is another way of creating the thread is using Inheritance; let’s see how:

Summary

To sum it up, in Python, threading is like having multiple workers (threads) doing different jobs at the same time. The program creates a custom thread to perform a specific task concurrently with the main thread. Each thread works independently, allowing the main thread to continue its own tasks. Threading is a handy tool to make programs faster by multitasking effectively.

Related Video https://www.youtube.com/watch?v=GCsCnBnXly8

Official Website https://pythonbricks.pythonanywhere.com/

Youtube Channel https://www.youtube.com/@PythonBricks

Telegram https://t.me/pythonbricks

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *