简体   繁体   English

在内部线程如何工作?

[英]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; 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? 意味着为什么每次运行时上述代码的输出都不固定,所以“ x”和“ y”的顺序不同?

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). 基本调度程序可以是运行高分辨率计时器的程序(例如,每100毫秒执行一次任务切换)。 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 (ie 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. 您所看到的是该调度程序中系统的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM