模拟售票系统
原创
©著作权归作者所有:来自51CTO博客作者微笑@鼠的原创作品,请联系作者获取转载授权,否则将追究法律责任
以下是模拟系统的代码:
import threading
import time
import random
class buyer(object):
def __init__(self,name='',number=1):
self.name = name
self.number = number
lock = threading.Lock()
class computer(object):
def __init__(self,count=0):
self.count = count
def query_with_buyer_info(self,other):
print('正在为{}查询剩余票数'.format(other.name))
time.sleep(random.randint(1,3))
lock.acquire()
if self.count >= other.number:
print('有票,{}可以购买'.format(other.name))
time.sleep(random.randint(1,3))
self.count -= other.number
else:
print('你所购买的票已售空')
lock.release()
hanmeimei = buyer('韩梅梅',2)
houzi = buyer(' 猴子',5)
com = computer(7)
thread1 = threading.Thread(target=com.query_with_buyer_info,name='thread1',args=(hanmeimei,))
thread2 = threading.Thread(target=com.query_with_buyer_info,name='thread2',args=(houzi,))
thread1.start()
thread2.start()
仅供参考