开发者

Internally how does a thread work?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜