Open In App

Different way to create a thread in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 01 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads