多线程经典问题-----乘客做公交车问题解答3
2013-12-15 20:34
459 查看
问题描述:
乘客乘坐公交车问题,司机,乘客,售票员协同工作,通过多线程模拟三者的工作。
司机:开车,停车
售票员:打开车门,关闭车门
乘客:上车,下车
用Python的Event做线程同步通信,代码如下:
输出结果如下:
乘客乘坐公交车问题,司机,乘客,售票员协同工作,通过多线程模拟三者的工作。
司机:开车,停车
售票员:打开车门,关闭车门
乘客:上车,下车
用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 "司机: 公交车开始行驶....." time.sleep(5) currentStationIndex += 1 print "司机: 到站 ",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 "售票员关闭车门" 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()
输出结果如下:
相关文章推荐
- 多线程经典问题-----乘客做公交车问题解答1
- 多线程经典问题-----乘客做公交车问题解答2
- 多线程经典问题-----乘客做公交车问题解答3
- Python多线程经典问题之乘客做公交车算法实例
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 多线程经典问题1——主线程子线程交替问题
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 多线程编程的几个经典问题
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 多线程之偷了个懒之经典生产消费问题
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 在Oracle下开发Java程序的经典问题解答
- 秒杀多线程第四篇 一个经典的多线程同步问题
- 经典售票的多线程安全问题
- 小问题可能存在大问题,希望大神帮忙解答。Spark本地运行模式中单线程与多线程问题之setMaster("local")可以运行,但是设置成setMaster("local[3]")或setMaste