Why does the main thread's output come first in C#?

前端 未结 7 2183
半阙折子戏
半阙折子戏 2021-02-04 23:41

I wrote this little program:

class Program
{
    static void Main(string[] args)
    {
        Thread t = new Thread(WriteX);
        t.Start();

        for (in         


        
7条回答
  •  一生所求
    2021-02-05 00:09

    You say:

    "It is weird for me, because the t thread starts first then the main continues.".

    This is not true. The "main" tread is already running. When t.Start(); is executed, the OS is told t is in the running state. The OS will then schedule execution time for the thread "soon". This is something else than the OS is instructed to stop execution of this thread until thread t is started. In other words, when Start returns, there is no guarantee that the thread has already started executing.

提交回复
热议问题