threading
threading1.py
import time from threading import Thread def sleepytime(threads): print("Thread " + str(threads) + " going to sleep.\n") time.sleep(5) print("Thread " + str(threads) + " waking up.\n") for threads in range(10): t = Thread(target=sleepytime, args=(threads,)) t.start()
threading2.py
import time from threading import Lock, Thread balance= 0 lock = Lock() def deposit(amount): global balance global lock print("Queued transaction...\n") lock.acquire() time.sleep(3) balance += amount print("Deposited:",str(amount),"Balance:",str(balance)) lock.release() a = Thread(target=deposit, args=(5,)) b = Thread(target=deposit, args=(10,)) a.start() b.start()
threading3.py
import time from threading import Lock, Thread balance= 0 lock = Lock() def deposit(amount): global balance global lock print("Queued transaction...\n") lock.acquire() time.sleep(3) balance += amount print("Deposited:",str(amount),"Balance:",str(balance)) lock.release() a = Thread(target=deposit, args=(5,)) b = Thread(target=deposit, args=(10,)) a.start() b.start() a.join() #allows waiting till finished b.join() #allows waiting till finished print("finished program execution.")