import threading
import time
dog_basin = threading.BoundedSemaphore(2) #dog_basin 代表狗盆
def eat(name,delay,food):
for i in range(5):
print("{}吃了{}个{}\t".format(name,i+1,food))
time.sleep(delay)
class Mythread(threading.Thread):
def __init__(self,name,delay,food):
threading.Thread.__init__(self)
self.name,self.delay,self.food = name,delay,food
def run(self):
try:
dog_basin.acquire()
print("{}正在使用狗盆\n".format(self.name))
eat(self.name,self.delay,self.food)
print("{}用完狗盆了\n".format(self.name))
dog_basin.release()
except Exception as e:
print(e)
thread1=Mythread("小黑",2,"骨头")
thread1.start()
threads=[]
threads.append(thread1)
for t in threads:
t.join()
print("线程执行结束")