From bd742a42920b0e2a9893d5b377e9e746aaeedd2c Mon Sep 17 00:00:00 2001 From: Kyle K Date: Wed, 7 Oct 2020 19:52:53 -0500 Subject: python threading example --- thread-ex1.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 thread-ex1.py diff --git a/thread-ex1.py b/thread-ex1.py new file mode 100644 index 0000000..522f535 --- /dev/null +++ b/thread-ex1.py @@ -0,0 +1,33 @@ +import threading +import time + +exitFlag = 0 + +class myThread (threading.Thread): + def __init__(self, threadID, name, counter): + threading.Thread.__init__(self, name=name) + self.threadID = threadID + #self.name = name + self.counter = counter + def run(self): + print("Starting " + self.name) + print_time(self.name, 5, self.counter) + print("Exiting " + self.name) + +def print_time(threadName, counter, delay): + while counter: + if exitFlag: + threadName.exit() + time.sleep(delay) + print("%s: %s" % (threadName, time.ctime(time.time()))) + counter -= 1 + +# Create new threads +thread1 = myThread(1, "Thread-One", 1) +thread2 = myThread(2, "Thread-Two", 2) + +# Start new Threads +thread1.start() +thread2.start() + +print("End of Program. Exiting Main Thread") \ No newline at end of file -- cgit v1.2.3