<p>We have found that several CPU intensive queries mean that our API server no longer responses to simple requests. The API server is an .net core application with kestrel, which is executed in a Kubernetes cluster.
However, if the application is running on a Windows or Linux host, the task prioritization seems to work perfectly. The service is responding even if there are dozens of CPU intensive requests. So there seems to be a significant difference between the Docker environment and the host environment.</p>
<p>I use this API method for test purposes:</p>
<pre><code>public void SimulateHighCpuLoad()
{
    var previousPriority = Thread.CurrentThread.Priority;

    try
    {
        Thread.CurrentThread.Priority = ThreadPriority.Lowest;

        var until = DateTime.Now.AddSeconds(30);
        var num = 0L;
        var random = new Random();

        // do senseless work for 30 seconds
        while (DateTime.Now &lt; until)
        {
            num = (random.Next() + Environment.TickCount + num) % (random.Next(10000) + 1);
            num *= num++;
        }
    }
    finally
    {
        Thread.CurrentThread.Priority = previousPriority;
    }
}
</code></pre>
<p>My goal is to prioritize CPU intensive methods lower so that the application can always respond to other requests (such as health requests for the LivenessProbe).
The <a href="https://learn.microsoft.com/dotnet/api/system.threading.thread.priority?view=netcore-3.1" rel="nofollow noreferrer">Thread.Priority</a> seems to be completely ignored within docker enviroment</p>

Thomas
<p>According to <a href="https://www.reddit.com/r/docker/comments/fszrss/understanding_processthread_priorities_and/" rel="nofollow noreferrer">this forum thread</a>, we are not the only ones with this problem.
Thread scheduling seems to work fundamentally differently within a Docker container (or K8s cluster).</p>
<p>We tried a lot and were able to solve the problem with a mixture of throttling and <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.sleep" rel="nofollow noreferrer">Thread.Sleep(0)</a>.</p>
<p>Note the following documentation for Thread.Sleep</p>
<blockquote>
<p>If the value of the millisecondsTimeout argument is zero, the thread
relinquishes the remainder of its time slice to any thread of equal
priority that is ready to run</p>
</blockquote>
<p>Without <code>Thread.Sleep(0)</code>, CPU-intensive threads cause that no other threads have been processed. No timers were called either. Setting a low thread priority is ineffective within a Docker container.</p>
