I am going through some code right now that is not mine. In the code there is a thread with the following code:
while (true)
{
Thread.sleep(int.MaxValue);
}
It also catches InterruptedException and goes right back into the loop, so the loop can't even be interrupted.
Does anyone know why this thread would exist, what the purpose is?
EDIT: The full code, for a little bit more context:
using IKVM.Attributes;
using java.lang;
using System;
using System.Runtime.CompilerServices;
namespace applicationNamespace
{
internal sealed class DedicatedServerSleepThread : Thread
{
internal DedicatedServer theDecitatedServer;
[MethodImpl(MethodImplOptions.NoInlining)]
internal DedicatedServerSleepThread(DedicatedServer dedicatedServer)
{
this.theDecitatedServer = dedicatedServer;
base.setDaemon(true);
this.start();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public override void run()
{
while (true)
{
try
{
while (true)
{
System.Threading.Thread.Sleep(int.MaxValue);
}
}
catch (System.Threading.ThreadInterruptedException)
{
}
}
}
static DedicatedServerSleepThread()
{
}
}
}
Note that the earlier code uses some non-standard libraries, so the lowercase sleep was valid. Specifically it was using ikvm libraries (which are based on java standard libraries, and used to cross-compile java programs to .net)
This was a java server program that I cross-compiled to .net bytecode and then decompiled. I wasn't sure if anyone had ever seen a thread dedicated to sleeping for any reason, and if so what the reason was. ta.speot.is for giving a really good answer.
Let's ignore the code you posted, since it won't compile, and just focus on whether a thread that sleeps forever serves any purpose.
There's three reasons I can think of for a thread whose job is to sleep, none of them are particularly legitimate.
To Do Nothing
The thread might exist to do nothing meaningful, forever. If someone tasked you with the job of creating a thread that should not do anything meaningful, ever, you might come up with that code.
To Keep The CLR/Process Alive
If it's a foreground thread, the thread will keep the process and CLR alive, even if the main thread completes. From MSDN:
A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.
To Cool Down
It's possible that by tying up a thread with sleep, other threads which can do meaningful work are not started as frequently (e.g. if they're being scheduled inside a threadpool or other environment). From The Daily WTF:
Due to a bunch of data-crunching speedups, the cpu's no longer have a chance to throttle down. The heat built up in the server, a fan controller fried and the cpu's cooked. As an alternative to purchasing redundant cooling, they made me slow the application down to previous performance levels with strategically placed sleeps.