Different way to create a thread in Python
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
from threading import *
def display() :
for i in range ( 10 ) :
print ( "Child Thread" )
Thread_obj = Thread(target = display)
Thread_obj.start()
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
from threading import *
class Mythread(Thread):
def run( self ):
for i in range ( 10 ):
print ( 'Child Thread' )
t = Mythread()
t.start()
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
from threading import *
class Gfg:
def method1( self ):
for i in range ( 10 ):
print ( 'Child Thread' )
obj = Gfg()
thread_obj = Thread(target = obj.method1)
thread_obj.start()
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
Share your thoughts in the comments
Please Login to comment...