Stuart A. Burkett wrote:Okay maybe I wasn't clear. By order of execution I did not mean the order in which the threads start. I was referring to the order in which time is allocated to each thread during which they can execute instructions.
If you were hoping each thraed would take it in turns to print out a word, then you are going to be disappointed. It might appear to work for a while if you put a longer delay in, but if you leave your program running long enough, you will probably get out of order words eventually.
Well, again, that is how threads work
If you are aware of various states of thread, a thread is picked from 'runnable' state by thread-scheduler, and then is executed (and its state is changed to 'running'). (Note: Please do not confuse between 'runnable' state and 'Runnable' interface)
When you invoke start method on a thread, you simply push that thread to runnable state.
Now, due to the way thread-scheduler works, there's no
guarantee that just because thread A is marked as runnable before thread B, thread A will enter running state before thread B. It can legally happen that when you say
and thread scheduler will choose to execute ThreadB first.
Also, it is thread-scheduler's task to do thread-context-switching. Its not always necessary that both thread will run in parallel (e.g. if execution of a thread is going to take very less time, then scheduler may decide to run another thread after this thread is completed).
I hope this answers your question.