开发者

What happens when a timer thread goes to sleep

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

static void Main(string[] args)
{
    _timer.Interval =开发者_StackOverflow 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);
}

What happens to the timer thread with such a long sleep? Does it get back to thread pool or we end up many sleeping threads?


The sleeping threads will return to the thread pool, but only after the sleep ends. A thread pool has a maximum number of threads, that means you could run out of threads to use, because all of them will be sleeping.

EDIT:

ThreadPool docs:

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜