Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Thread in Python:-
 
Thread is a collection of light-weight subprocess to execute the program, in the application, we will use Multithreading to implement multitasking.

using Multi-threading we can execute more than one process simultaneous in a single resource.

O/s is the best example of multi-threading because it can execute more than one program simultaneously.

When we want to create a game application then parallel execution is mandatory, which can be managed by the Multi-threading process.



Thread Life Cycle:-




Syntax of threading:-

import threading

class Classname(threading.Thread):
      functionality
      ........

import threading
import time
class ThreadExample(threading.Thread):
    def run(self):
        for i in range(1,10):
            print("Process is "+str(i))
            time.sleep(1)



t1 = ThreadExample()  #new state ,it will provide memory to store data
t1.start()  #start() is the predefine method which will convert thread process to runnable state to running state

Multithreading:-  If we start more then one Thread object simultaneous then it is called multithreading.
It is used to execute multiple programs set simultaneous.
 
import threading
import time
class ThreadExample(threading.Thread):
    def run(self):
        for i in range(1,10):
            print("Process is "+str(i))
            time.sleep(1)



t1 = ThreadExample()  #new state ,it will provide memory to store data
t1.start()  #start() is the predefine method which will convert thread process to runnable state to running state

t2 = ThreadExample()
t2.start()

t3 = ThreadExample()
t3.start()
            


Thread Process Synchronization:-


using this we can provide multi-threading execution is waiting for the state until the current thread process completed. when the process completed then join() will notify another thread and it will be executed .using the join()  method we can synchronize the thread process.



import threading
import time
class ThreadExample(threading.Thread):
    def run(self):
        for i in range(1,10):
            print("Process is "+str(i))
            time.sleep(1)



t1 = ThreadExample()  #new state ,it will provide memory to store data
t1.start()  #start() is the predefine method which will convert thread process to runnable state to running state
t1.join()
t2 = ThreadExample()
t2.start()
t2.join()
t3 = ThreadExample()
t3.start()
            



Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post