# -*- coding: utf-8 -*-
import threading
import time
import os
def booth(tid):
global num
while True:
lock.acquire() # 锁定
if num != 0:
num = num - 1
print("Thread_id: {} 剩余售票: {}".format(tid, num))
time.sleep(0.5)
else:
print("Thread_id: {} 票已售空".format(tid))
os._exit(0) # 票售完,退出程序
lock.release() # 释放锁
time.sleep(0.5)
num= 15 # 初始化票数
lock = threading.Lock() # 创建锁
if __name__ == ‘__main__‘:
# 设置了5个线程
for i in range(5):
t = threading.Thread(target=booth, args=(i,))
t.start()
原文:https://www.cnblogs.com/test-works/p/15349409.html