What is thread explain different ways 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.


What is a thread explain the different ways to create a thread in Python?

Threading speeds up program execution by allowing us to run parts of a program concurrently. So threading is a way that we can execute multiple pieces of code at the same time. There are two ways of creating threads in Python and those are; using a class or using a function.

What is a thread in Python?

Threads in python are an entity within a process that can be scheduled for execution. In simpler words, a thread is a computation process that is to be performed by a computer. It is a sequence of such instructions within a program that can be executed independently of other codes.

How do you create a thread in Python?

Use the Python threading module to create a multi-threaded application. Use the Thread[function, args] to create a new thread. Call the start[] method of the Thread class to start the thread. Call the join[] method of the Thread class to wait for the thread to complete in the main thread.

What is thread and its types in Python?

Thread is a single sequence stream within a process. Threads have same properties as of the process so they are called as light weight processes. Threads are executed one after another but gives the illusion as if they are executing in parallel. Each thread has different states.

Chủ Đề