diff options
author | Kyle K <kylek389@gmail.com> | 2020-10-07 19:52:53 -0500 |
---|---|---|
committer | Kyle K <kylek389@gmail.com> | 2020-10-07 19:52:53 -0500 |
commit | bd742a42920b0e2a9893d5b377e9e746aaeedd2c (patch) | |
tree | 198db3b169e5c6d8cb4bf3e7c58ec66b369fea4a | |
parent | 8923b28f60cd2b2aebb7ee4ec0c1d6ada627ef8f (diff) | |
download | PythonPractice-bd742a42920b0e2a9893d5b377e9e746aaeedd2c.tar.gz PythonPractice-bd742a42920b0e2a9893d5b377e9e746aaeedd2c.tar.bz2 PythonPractice-bd742a42920b0e2a9893d5b377e9e746aaeedd2c.zip |
python threading example
-rw-r--r-- | thread-ex1.py | 33 |
1 files changed, 33 insertions, 0 deletions
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 |