国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多線程經典問題之乘客做公交車算法實例

2019-11-25 16:17:53
字體:
來源:轉載
供稿:網友

本文實例講述了Python多線程經典問題之乘客做公交車算法。分享給大家供大家參考,具體如下:

問題描述:

乘客乘坐公交車問題,司機,乘客,售票員協同工作,通過多線程模擬三者的工作。
司機:開車,停車
售票員:打開車門,關閉車門
乘客:上車,下車

用Python的Event做線程同步通信,代碼如下:

# *-* coding:gb2312 *-*import threadingimport timestationName=("車站0","車站1","車站2","車站3","車站4","車站5","車站6")currentStationIndex = -1eventBusStop = threading.Event()eventClosedDoor = threading.Event()eventOpenedDoor = threading.Event()stationCount = len(stationName)class Passenger(threading.Thread):  def __init__(self,no,getonStation,getoffStation):    self.no =no    self.getonStation=getonStation    self.getoffStation=getoffStation    threading.Thread.__init__(self)  def run(self):    bExit= False    global currentStationIndex    global stationCount    bAlreadyGetOnStation = False    while not bExit:      eventOpenedDoor.wait()      if self.getonStation == currentStationIndex and bAlreadyGetOnStation == False:        print "乘客%d在%s上車" %(self.no,stationName[currentStationIndex])        bAlreadyGetOnStation =True      elif self.getoffStation == currentStationIndex:        print "乘客%d在%s下車" %(self.no,stationName[currentStationIndex])        bExit = True      time.sleep(1)class Driver(threading.Thread):  def run(self):    bExit= False    global currentStationIndex    global stationCount    while not bExit:      print "司機: 公交車開始行駛....."      time.sleep(5)      currentStationIndex += 1      print "司機: 到站 ",stationName[currentStationIndex]      eventBusStop.set()      eventClosedDoor.wait()      eventClosedDoor.clear()      if currentStationIndex == stationCount-1:        bExit= Trueclass Conductor(threading.Thread):  def run(self):    bExit= False    global currentStationIndex    global stationCount    while not bExit:      eventBusStop.wait()      eventBusStop.clear()      print "售票員打開車門:%s到了" %(stationName[currentStationIndex])      eventOpenedDoor.set()      time.sleep(5)      print "售票員關閉車門"      eventOpenedDoor.clear()      eventClosedDoor.set()      if currentStationIndex == stationCount-1:        bExit = Truedef test():  passPool=[]  passPool.append(Passenger(0,0,3))  passPool.append(Passenger(1,1,3))  passPool.append(Passenger(2,2,4))  passPool.append(Passenger(3,0,5))  passPool.append(Passenger(4,1,3))  passPool.append(Passenger(5,2,4))  passPool.append(Passenger(6,4,5))  passPool.append(Passenger(7,0,2))  passPool.append(Passenger(8,1,3))  passPool.append(Conductor())  passPool.append(Driver())  leng = len(passPool)  for i in range(leng):    passPool[i].start()if __name__=='__main__':  test()

輸出結果如下:

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python Socket編程技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 襄樊市| 菏泽市| 长寿区| 汤原县| 宁国市| 佛冈县| 喀什市| 双鸭山市| 兴化市| 洪洞县| 易门县| 万安县| 邻水| 资兴市| 北票市| 沁源县| 博白县| 麻阳| 武汉市| 霍林郭勒市| 巴南区| 措勤县| 宿松县| 阿巴嘎旗| 越西县| 新竹市| 凉山| 宁陕县| 安陆市| 丁青县| 锦州市| 女性| 拉孜县| 阿巴嘎旗| 河东区| 吉安市| 南投县| 安陆市| 将乐县| 靖安县| 板桥市|