c# - 计时器线程进入休眠状态会发生什么

标签 c# multithreading timer sleep

static readonly System.Timers.Timer _timer = new System.Timers.Timer();

static void Main(string[] args)
{
    _timer.Interval = 1000;
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
    _timer.Start();

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    Console.WriteLine(DateTime.Now);
    Thread.Sleep(600000); // 10 minutes
    Console.WriteLine(DateTime.Now);
}

如此长时间的 sleep 会对计时器线程产生什么影响?它回到线程池还是我们最终陷入了许多休眠线程?

最佳答案

sleep 线程将返回到线程池,但是仅在 sleep 结束后才返回。线程池具有最大数量的线程,这意味着您可能用完了线程,因为所有线程都将处于 sleep 状态。

编辑:

ThreadPool文档:

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.getmaxthreads.aspx

关于c# - 计时器线程进入休眠状态会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012350/

相关文章:

c# - 无法应用运算符 &&

java - 需要对 Jar PathFinder 示例中发生的丢失通知进行解释

c# - 使用多个参数启动任务的标准做法是什么

c - 在c中实现定时器回调

C# Timer 滴答加速,可能是由于不正确的使用

.net - 检测是否调用了垃圾收集器(.Net)

c# - 在异步/等待应用程序中获取所有堆栈跟踪

c# - 使用 C# 在 Azure 中创建资源组

c# - 在 C#(或一般的 .NET)中,您可以屏蔽通过属性抛出异常的调用堆栈级别吗?

java - Tomcat 7 Servlet 产生一个线程