I played around with thread in Python 3x. Below is my code.
import threading
def hello_world2():
threading.Timer(7.0, hello_world2).start()
print("22222222!")
def hello_world():
threading.Timer(5.0, hello_world).start()
print("Hello, World!")
hello_world2()
hello_world()
However, I am surprised with the output printed because it has two possible scenarios for output that I observed.
First one is
Hello, World!
22222222!
Hello, World!
22222222!
Hello, World!
Hello, World!
22222222!
It printed out Hello, World!
first while in my code I specifically wrote to print 22222222!
first.
The second scenario is
22222222!
Hello, World!Hello, World!
22222222!
Hello, World!
22222222!
Hello, World!
Hello, World!
22222222!
Although it printed 22222222!
first, it didn't start a new line before printing the second Hello, World!
on the second line.
So, I want to know what's happened here to cause this kind of behaviour and will I be able to control it to make it behave the way I intend it to do.
NOTE: My question doesn't primarily focus on printing but rather asking about a general behavior of multithreaded programme. If I use multiple threads, would my programme have a dodgy or undesirable sequence of execution? Would this kind of strange behaviour exist even if I don't print out (compared to run a single thread)
Thank you.