Creating multiple Custom threads in python

Previous
Next

How to execute different logics from different threads?

  • We can define run() method only once in a Thread class.
  • To execute different logics, different run() methods need to override.
  • It is possible by defining more than one thread class and override run() method in each class with different logic.
from threading import Thread
import time

class Custom1(Thread):
    def run(self):
        for i in range(1,11):
            print("custom1 :" + str(i))
            time.sleep(1)
        return

class Custom2(Thread):
    def run(self):
        for i in range(50,71):
            print("custom2 :" + str(i))
            time.sleep(1)
        return

class Custom3(Thread):
    def run(self):
        for i in range(100,90,-1):
            print("custom3 :"+str(i))
            time.sleep(3)
        return
    
class Default:
    def main():
        c1 = Custom1()
        c1.start()
        c2 = Custom2()
        c2.start()
        c3 = Custom3()
        c3.start()
        return

Default.main()
Previous
Next

Add Comment

Courses Enquiry Form