# L-28 MCS 507 Wed 26 Oct 2011 : hello_threading.py

# Illustration on the use of the Thread class,
# available in the threading module.

import threading
import time
import random

class HelloThread(threading.Thread):
   """
   hello world with threads
   """
   def __init__(self,t):
      "initializes thread with name t"
      threading.Thread.__init__(self,name=t)
      print t + " is born "

   def run(self):
      "says hello and sleeps awhile"
      t = self.getName()
      print "hello from " + t
      r = random.randint(1,6)
      time.sleep(r)
      print t + " slept %d seconds" % r

def main():
   "starts three threads"
   t1 = HelloThread("first thread")
   t2 = HelloThread("second thread")
   t3 = HelloThread("third thread")
   print "starting threads"
   t1.start()
   t2.start()
   t3.start()
   print "threads started"

if __name__ == "__main__": main()
