简体   繁体   English

.NET控制台应用程序在Console.ReadLine()上阻止时会发生什么?

[英]What happens when a .NET console app blocks on Console.ReadLine()?

I had an interesting interview question today. 今天我有一个有趣的面试问题。 Consider you have the following console application: 考虑您具有以下控制台应用程序:

static void Main(string[] args) 
{
    Console.WriteLine("Hello world");
    Console.ReadLine();
}

When the Console.ReadLine() line is reached execution is suspended and the program waits for input from the keyboard. 当到达Console.ReadLine()行时,将暂停执行,程序将等待键盘输入。 How many threads are there at this point and what state are they in, eg Running, Suspended, etc. ? 此时有多少个线程,它们处于什么状态,例如正在运行,已挂起等?

I suppose what the interviewer was after was awareness/understanding of the threads that make up a .NET console app and how they work together to interact with the IO subsystem of the underlying OS. 我想访问者的目的是了解/理解组成.NET控制台应用程序的线程,以及它们如何协同工作以与基础OS的IO子系统进行交互。

There's no definite number. 没有确切的数字。

Your code is only running on one thread - the main thread. 您的代码仅在一个线程(主线程)上运行。 Calling Console.ReadLine doesn't create a new thread, it just starts an I/O request and waits for it to be processed - this requires no threads, but since you're using the synchronous API, there's no way to release your thread, so it just blocks. 调用Console.ReadLine不会创建新线程,它只是启动一个I / O请求并等待它被处理-这不需要线程,但是由于您使用的是同步API,因此无法释放线程,所以它只是块。 If the interviewer wanted just one number, this is the number - it's the only application thread you have, everything else is an implementation detail. 如果面试官只想要一个数字, 那就是数字 -这是您拥有的唯一应用程序线程,其他所有东西都是实现细节。

There's a lot of infrastructure threads - the Garbage Collector threads are the main ones, and some of those are created and destroyed all the time. 基础结构线程很多-垃圾收集器线程是主要的线程,其中一些始终被创建和销毁。

Finally, there's the threads somewhere in between - most notably, the thread pool. 最后,线程之间存在某些地方-最值得注意的是线程池。 Since you're not using the thread-pool, it will have the default number of threads - unless the configuration says otherwise, this is usually two threads per logical CPU core. 由于您未使用线程池,因此它将具有默认的线程数-除非配置另有说明,否则通常每个逻辑CPU核心两个线程。

The reality goes even deeper. 现实变得更深了。 For example, .NET threads are managed threads, and they may skip between "native" threads at the will of the runtime. 例如,.NET线程是托管线程,它们可以在运行时随意在“本机”线程之间跳过。 However, this is something you never really need to care about unless you're writing your own .NET runtime or an operating system :) Or, doing heavy interop with native code that depends on native thread-affinity (again, not very common). 但是,除非您正在编写自己的.NET运行时或操作系统,否则这是您真正不需要关心的事情:)或者,使用依赖于本机线程亲和力的本机代码进行大量互操作(再次,这不是很常见) 。

I suspect the main purpose of the question is to get you talking about how the Windows threading model works, how .NET handles threads and what kinds of threads there are in a typical windows/.NET application. 我怀疑这个问题的主要目的是让您讨论Windows线程模型如何工作,.NET如何处理线程以及典型的Windows / .NET应用程序中存在哪些线程。 But mainly, to get you to talk :P 但主要是,让您交谈:P

I agree with Luuan, therefore, depending on what your interviewer meant, there is for example VisualStudio threads running in addition to the Main thread. 我同意Luuan,因此,根据您的面试官的意思,例如,除了Main线程外,还有VisualStudio线程在运行。 BTW, this thread can be disabled (In VS2015: Project Properties > Debug tab > uncheck Enable the Visual Studio hosting Process) 顺便说一句,可以禁用此线程(在VS2015中:在“项目属性”>“调试”选项卡中,取消选中“启用Visual Studio托管进程”)

If you are using Visual Studio, you should be able to see the threads during debugging. 如果使用的是Visual Studio,则应该能够在调试过程中看到线程。

The running code: 运行代码:

static void Main(string[] args)
{
    Console.WriteLine("Hello world");           
    Console.ReadLine();
}

The Thread Window (In VS2015: Debug -> Windows -> Threads): 线程窗口(在VS2015中:调试-> Windows->线程):

在此处输入图片说明

I added a new thread to your example: 我在您的示例中添加了一个新线程:

The running code: 运行代码:

static void Main(string[] args)
{
    Console.WriteLine("Hello world");
    new Thread(DoSomethingElse).Start();            
    Console.ReadLine();
}

private static void DoSomethingElse()
{
    while (true)
    {
        Thread.SpinWait(100);
    }
}

The Thread Window: 线程窗口:

在此处输入图片说明

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

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