Add multithreadedMultiplication.py

master
Caleb Fontenot 2021-03-23 08:53:11 +07:00
parent f4f5d78f94
commit 0b930459c8
1 changed files with 26 additions and 0 deletions

@ -0,0 +1,26 @@
#!/bin/python
import threading
from time import sleep
baseGlobal = {0: 0, 1: 0}
sleepTime = 1
#Initialize functions that we will execute as threads:
def mult(num1,threadOffset):
global baseGlobal,sleepTime
while True:
num1 *= 2
baseGlobal[threadOffset] = num1
sleep(int(sleepTime))
def printOutput():
while True:
print("Multiples of 8: "+str(baseGlobal[0]), end="")
print(" Multiples of 32: "+str(baseGlobal[1]), end="\r", flush=True)
print("Multithreaded Multiplication"+"\n")
if __name__ == "__main__":
#Create Threads
t1 = threading.Thread(target=mult, args=(8,0))
t2 = threading.Thread(target=mult, args=(32,1))
t3 = threading.Thread(target=printOutput, args=())
#Start Threads
t1.start()
t2.start()
t3.start()