-2

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.

mirhagk
  • 1,255
  • 3
  • 14
  • 28

3 Answers3

3

The code, as written (except with Sleep capitalized), will only serve the purpose of making a thread that does nothing, and uses up some extra memory (for the thread's stack data).

It's possible that the thread was intended to perform some other function, but it's likely something that should have been handled in another manner.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

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.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • I would hope that it wasn't written for purpose 1, but knowing that the code is written horrendously in many places, I wouldn't be surprised if it was. Number 2 kind of might make sense, but the program has setDaemon (which is java's equivalent of setting to background thread), so the process wouldn't be left alive. I guess it's number 3 then. Although I was also thinking it could of been that the programmers wanted to add multi-threading later on, and built it to save the spot or something. It seems like it was actually thought out and planned to be there, not just being dumb. – mirhagk Apr 20 '13 at 12:13
1

From what you have shown us it just creates a thread and makes it sleep forever. Without more context we can't tell you anything else

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Alright thanks, just saw this in the source code of a major project I decompiled. It seems rather strange and I really don't understand why it exists, thought someone might've known. – mirhagk Apr 20 '13 at 01:43