Different way to create a thread in Python

Last Updated : 1 Oct, 2020

A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an Operating System.Ā 

There are various ways to create a thread:

1) Create a Thread without using an Explicit function:Ā 

By importing the module and creatingĀ the ThreadĀ class object separately we can easily create a thread. It is a function-oriented way of creating aĀ thread.Ā 

Python3
# Import required modules
from threading import *    


# Explicit function
def display() :                
  for i in range(10) :
    print("Child Thread")


# Driver Code    
    
# Create object of thread class    
Thread_obj = Thread(target=display)        

# Executing child thread
Thread_obj.start()            

# Executing main thread
for i in range(10):            
  print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread  

In the above example, we have created an explicit functionĀ display()Ā which printsĀ Child ThreadĀ 10 times. Then we created aĀ ThreadĀ class object namedĀ Thread_objĀ using theĀ threadingĀ module. The firstĀ ThreadĀ is targeting theĀ display()Ā method i.e.Ā display()Ā method will be executed by thisĀ ThreadĀ object and the mainĀ thread(the second one) is targeting theĀ forĀ loop and will be responsible for executing by the MainĀ ThreadĀ 10 times.

NOTE:Ā Here which thread will get chance first (Main ThreadĀ orĀ Child Thread) depends on the Thread Scheduler present in Python Virtual Machine (PVM), so we can't predict the output.Ā 

2) Create Thread by extending Thread Class :Ā 

In this method, we will extend theĀ threadĀ class from theĀ threadingĀ module. This approach of creating a thread is also known as Object-Oriented Way.Ā Ā 

Python3
# Import required module
from threading import *


# Extending Thread class
class Mythread(Thread):

    # Target function for thread
    def run(self):
        for i in range(10):
            print('Child Thread')


# Driver Code
            
# Creating thread class object
t = Mythread()

# Execution of target function
t.start()

# Executed by main thread
for i in range(10):
    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

In the above example, we create a class that extends theĀ ThreadĀ class, inside this class, we must overrideĀ the run()Ā method which will be the target function of ourĀ Child Thread, when theĀ start()Ā method is called it initiates the execution of theĀ run()Ā method(Target Function) of theĀ thread.Ā 

3. Create Thread without extending Thread Class :Ā 

Another Object-Oriented Way of creatingĀ ThreadĀ is by not extending any thread class to create Thread.

Python3
# Import required modules
from threading import *


# Creating class
class Gfg:

    # Creating instance method
    def method1(self):
        for i in range(10):
            print('Child Thread')


# Driver Code
            
# Creating object of Gfg class
obj = Gfg()

# Creating object of thread by
# targeting instance method of Gfg class
thread_obj = Thread(target=obj.method1)

# Call the target function of threa
thread_obj.start()

# for loop executed by main thread
for i in range(10):
    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

In the above program, we separately create an object of thread class andĀ GfgĀ class, and whenever we create an object ofĀ threadĀ class that time we have to mention the target function as well. The thread class object targets the instance method of theĀ Gfg class. To start the execution of the target function we must call theĀ start()Ā method.

Comment