-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_threading_test.py
More file actions
90 lines (78 loc) · 2.8 KB
/
python_threading_test.py
File metadata and controls
90 lines (78 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# *-* coding:gb2312 *-*
import threading
import time
stationName=("stop 0","Stop 1","Stop 2","Stop 3","Stop 4","Stop 5","Stop 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 "Passenger%d onboard at %s stop" %(self.no,stationName[currentStationIndex])
bAlreadyGetOnStation =True
elif self.getoffStation == currentStationIndex:
print "Passenger %d get off at %s stop" %(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 "Driver: Bus is leaving from the station....."
time.sleep(5)
currentStationIndex += 1
print "Driver: arrived at the station: ",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 "Conductor opened the door: %s arrived" %(stationName[currentStationIndex])
eventOpenedDoor.set()
time.sleep(5)
print "Conductor closed the door"
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()