卖火车票
import threading as th
import random as ran
ticketPlace=['北京','上海','杭州','天津']
lock=th.Lock()
#创建火车票编号
ticketIds=[]
for i in range(1,101):
id='南京--第'+str(i).zfill(5)+'坐'
ticketIds.append(id)
def fun():
while True:
lock.acquire()#上锁
ticketId=ran.choice(ticketIds)
print(th.current_thread().name+'卖出编号为'+ticketId+'还剩:'+str(len(ticketIds)-1)+'票')
ticketIds.remove(ticketId)
if len(ticketIds)==0:
print('已售馨!')
break
lock.release()#解锁
for i in range(4):#创建四个线程一起买
th.Thread(target=fun,name=ticketPlace[i]).start()
# fun方法传参的时候不能加括号,如果加了后面的参数就穿不进去了