I would like to add that the code of the main method works in the main thread, and Thread t does not block the execution of Main in your example. Therefore, a line x = y + 1
can run faster than the body of Thread t (as @davidxxx already pointed out).
You can observe other behavior if added t.join()
aftert.start():
t.start();
t.join();
In this situation, the main thread will wait for Thread t to complete , and the output will be 1.
source
share