欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python多線程經(jīng)典問題之乘客做公交車算法實(shí)例

 更新時(shí)間:2017年03月22日 11:49:35   作者:聰明的狐貍  
這篇文章主要介紹了Python多線程經(jīng)典問題之乘客做公交車算法,簡(jiǎn)單描述了乘客坐公交車問題并結(jié)合實(shí)例形式分析了Python多線程實(shí)現(xiàn)乘客坐公交車算法的相關(guān)技巧,需要的朋友可以參考下

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

問題描述:

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

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

# *-* coding:gb2312 *-*
import threading
import time
stationName=("車站0","車站1","車站2","車站3","車站4","車站5","車站6")
currentStationIndex = -1
eventBusStop = 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 "司機(jī): 公交車開始行駛....."
      time.sleep(5)
      currentStationIndex += 1
      print "司機(jī): 到站 ",stationName[currentStationIndex]
      eventBusStop.set()
      eventClosedDoor.wait()
      eventClosedDoor.clear()
      if currentStationIndex == stationCount-1:
        bExit= True
class 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 "售票員關(guān)閉車門"
      eventOpenedDoor.clear()
      eventClosedDoor.set()
      if currentStationIndex == stationCount-1:
        bExit = True
def 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()

輸出結(jié)果如下:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 不到20行代碼用Python做一個(gè)智能聊天機(jī)器人

    不到20行代碼用Python做一個(gè)智能聊天機(jī)器人

    小編先向大家介紹一下本次運(yùn)用到的python庫,本次項(xiàng)目主要運(yùn)用到的庫有wxpy和chatterbot。對(duì)Python做一個(gè)智能聊天機(jī)器人的相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • Django values()和value_list()的使用

    Django values()和value_list()的使用

    這篇文章主要介紹了Django values()和value_list()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python之string編碼問題

    Python之string編碼問題

    這篇文章主要介紹了Python之string編碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python FtpLib模塊應(yīng)用操作詳解

    Python FtpLib模塊應(yīng)用操作詳解

    這篇文章主要介紹了Python FtpLib模塊應(yīng)用操作,結(jié)合實(shí)例形式詳細(xì)分析了FtpLib模塊的功能、配置、應(yīng)用相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2019-12-12
  • Python?Timer和TimerFPS計(jì)時(shí)工具類

    Python?Timer和TimerFPS計(jì)時(shí)工具類

    這篇文章主要介紹了Python?Timer和TimerFPS計(jì)時(shí)工具類,文章分享得代碼內(nèi)容詳細(xì),具有一的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • python自動(dòng)化發(fā)送郵件實(shí)例講解

    python自動(dòng)化發(fā)送郵件實(shí)例講解

    在本篇文章里小編給大家分享了一篇關(guān)于python自動(dòng)化發(fā)送郵件實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • python 實(shí)現(xiàn)將list轉(zhuǎn)成字符串,中間用空格隔開

    python 實(shí)現(xiàn)將list轉(zhuǎn)成字符串,中間用空格隔開

    今天小編就為大家分享一篇python 實(shí)現(xiàn)將list轉(zhuǎn)成字符串,中間用空格隔開,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python反扒機(jī)制的5種解決方法

    python反扒機(jī)制的5種解決方法

    這篇文章主要介紹了python反扒機(jī)制的5種解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python開發(fā)利器之ulipad的使用實(shí)踐

    python開發(fā)利器之ulipad的使用實(shí)踐

    Ulipad是一個(gè)國人limodou編寫的專業(yè)Python編輯器,它基于wxpython開發(fā)的GUI(圖形化界面)。下面這篇文章主要介紹了python開發(fā)利器之ulipad的使用實(shí)踐,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • 使用Python從圖像中提取文本OCR庫的操作詳解

    使用Python從圖像中提取文本OCR庫的操作詳解

    光學(xué)字符識(shí)別(OCR, Optical Character Recognition)是一種將印刷或手寫文本從圖像、PDF或掃描件中提取為機(jī)器可讀文本的技術(shù),使用Python進(jìn)行OCR處理,開發(fā)者可以輕松調(diào)用各種OCR庫,所以本文將給大家介紹使用Python從圖像中提取文本OCR庫的操作
    2024-08-08

最新評(píng)論