开发者

What happens when a timer thread goes to sleep

开发者 https://www.devze.com 2023-03-06 07:16 出处:网络
static readonly System.Timers.Timer _timer = new System.Timers.Timer(); static void Main(string[] args)
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

精彩评论

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

关注公众号