Skip to content

Thread Pool Sizing

ZjzMisaka edited this page May 10, 2024 · 9 revisions

The thread count adjusts dynamically based on the workload experienced by the threads in the pool.

Maximum number of threads can be setted through PowerPoolOption.MaxThreads (the default value is the number of processor cores * 2), at which point the number of threads maintained by the thread pool will stabilize at this count, until the DestroyThreadOption property is set.

After setting the DestroyThreadOption, the initial number of threads in the thread pool will be set to DestroyThreadOption.MinThreads. When all threads are busy and a new work is added, new threads will be created until the number reaches PowerPoolOption.MaxThreads.

However, when a thread becomes idle, it will remain idle and wait for new works until the wait time exceeds DestroyThreadOption.KeepAliveTime. After this time, the thread will be released unless the current number of threads is equal to DestroyThreadOption.MinThreads.

PowerPool powerPool = new PowerPool(new PowerPoolOption()
{
    MaxThreads = 8,
    DestroyThreadOption = new DestroyThreadOption() { MinThreads = 4, KeepAliveTime = 3000 }
});

thread starvation

If there are long-running works (i.e., works where WorkOption.LongRunning is true), the maximum number of threads in the thread pool will be MaxThreads plus the number of long-running works. This design specifically addresses thread starvation by allowing long-running works to operate on additional threads, ensuring that short-term works retain access to the thread pool and can be processed without undue delay.