Q:

Python program for multithreading with class

0

A thread is a block of code that can run independently.

Multithreading is the process of running multiple threads simultaneously to increase the program's speed.

Threads can be run using class, by making a class object a thread that can run independently.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python program for multithreaded with class

import threading
import time

class Car(threading.Thread):
   def init(self):
       self.i=0

   def run(self):
      i=1
      while(i<=10):
         if(self.getName()=="Ciaz"):
           time.sleep(1)
         if (self.getName() == "Swift" and self.i >= 3):
             break

         print(self.getName(),"Car Is Running....")
         i+=1
         self.i+=1

swift=Car()
swift.init()
swift.setName("Swift")
swift.start()

ciaz=Car()
ciaz.init()
ciaz.setName("Ciaz")
ciaz.start()

Output:

Swift Car Is Running....
Swift Car Is Running....
Swift Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....
Ciaz Car Is Running....

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now