# L-19.0 MCS 260 Wed 30 July 2014 : hello_threading.py
"""
Illustration on the use of the Thread class,
available in the threading module.
"""

from threading import Thread
from time import sleep
from random import randint

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

    def run(self):
        "says hello and sleeps awhile"
        name = self.getName()
        print "hello from " + name
        rnd = randint(3, 6)
        sleep(rnd)
        print name + " slept %d seconds" % rnd

def main():
    "starts three threads"
    one = HelloThread("1st thread")
    two = HelloThread("2nd thread")
    three = HelloThread("3rd thread")
    print "starting threads"
    one.start()
    two.start()
    three.start()
    print "threads started, waiting to finish"
    one.join()
    two.join()
    three.join()

if __name__ == "__main__":
    main()
