铁路上只有两列火车(同时)的最简单方法是什么?我的英语不好。这是我解释它的唯一方法。我知道我应该使用队列?我找不到我的语言的信息
谢谢!
1>去,2>去。3,4等等。1>完成,3>去(第四个还在等待)..
from threading import Thread
import time
import random
def trains(city):
print city, 'start'
for count in range(1,3):
delay = random.randrange(5,10)
print city, 'delay', delay
time.sleep(delay)
print city, 'end'
cities = ['prague', 'london', 'berlin', 'moscow']
threadlist = []
for city in cities:
t = Thread(target=trains, args=(city,))
t.start()
threadlist.append(t)
for b in threadlist:
b.join()