我只是玩弄多線程,但我似乎無法得到它的工作。我看過其他問題,但沒有一個真正幫助我。這裏是我到目前爲止的代碼:Python 3.4中的多線程如何工作?
import threading, time
def hello():
for i in range(1,5):
time.sleep(1)
print("Hello")
def world():
for i in range(1,5):
time.sleep(1)
print("World")
newthread = threading.Thread(hello())
newthread.daemon = True
newthread.start()
otherthread = threading.Thread(world())
otherthread.daemon = True
otherthread.start()
print("end")
我希望得到的東西,如:
Hello
World
Hello
World
Hello
World
Hello
World
end
而是我得到:
Hello
Hello
Hello
Hello
World
World
World
World
end
感謝您的回答,完美工作,並且在啓動線程之間添加了一小段延遲,以使輸出正確。 :) –