Multiple threads printing at different intervals. : Synchronize : Thread PYTHON TUTORIALS


PYTHON TUTORIALS » Thread » Synchronize »

 

Multiple threads printing at different intervals.


import threading
import random
import time

class PrintThreadthreading.Thread ):
   def __init__self, threadName ):
      threading.Thread.__init__self, name = threadName )
      self.sleepTime = random.randrange1)
      print "Name: %s; sleep: %d" self.getName(), self.sleepTime )

   def runself ):
      print "%s going to sleep for %s second(s)" self.getName(), self.sleepTime )
      time.sleepself.sleepTime )
      print self.getName()"done sleeping"

thread1 = PrintThread"thread1" )
thread2 = PrintThread"thread2" )
thread3 = PrintThread"thread3" )

thread1.start()   # invokes run method of thread1
thread2.start()   # invokes run method of thread2
thread3.start()   # invokes run method of thread3



    Related Scripts with Example Source Code in same category :

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Thread
» Synchronize