Thread t = new Thread (WriteY);
t.Start();            
for (int i = 0; i < 1000; i++) Console.Write ("x");
static void WriteY()
{
    for (int i = 0; i < 1000; 开发者_Python百科i++) Console.Write ("y");
} 
internally How does thread work ? means why output of above code is not fix every time i run, sequence of 'x' and 'y' is different?
All multitasking systems have a scheduler. A scheduler decides what unit of work will execute next. A basic scheduler can be something that runs of a high resolution timer (like, say, every 100ms, a task switch happens). Clearly, modern implementations are much more complicated than that.
That said, most modern threading implementations rely on a scheduler within the kernel. Many of these schedulers are NOT deterministic. That is, there is no guarantee that a context switch (i.e. a switch between runnable instances managed by the scheduler) will happen at any specific time.
What you are seeing are the discrepancies in that scheduler for your system.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论