import threading
import time
def spaghetti():
time.sleep(2)
print('Its done')
def eating_lunch(lock_x:threading.Lock, lock_y:threading.Lock):
lock_x.acquire()
lock_y.acquire()
spaghetti()
lock_y.release()
lock_x.release()
def philsophers():
forks = list()
for i in range(5):
fork = threading.Lock()
forks.append(fork)
philsophers = list()
for i in forks:
a = forks.index(i) + 1
if a != 5:
j = forks[a]
else:
j = forks[0]
philsopher = threading.Thread(target=eating_lunch, args=(i, j))
philsopher.start()
philsophers.append(philsopher)
for ph in philsophers:
ph.join()
if __name__ == '__main__':
start_time = time.time()
philsophers()
print(time.time() - start_time)