summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java
blob: b270895c6ef5c1f0089dd2a9c2ea69a70d445d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.keuin.kbackupfabric.util;

public class ThreadingUtil {
    public static int getRecommendedThreadCount() {
        int coreCount = Runtime.getRuntime().availableProcessors();

        // if the cores are too few, we regress to single thread
        if (coreCount <= 2)
            return 1;

        // we have multiple cores, but not too many
        if (coreCount == 3)
            return 2;

        // if we have multiple core, but not too many, we use a half
        if (coreCount <= 6) // 4, 5, 6 -> 3, 3, 4
            return (coreCount + 2) / 2;

        // cores are sufficient, we use almost all of them, except a fixed count remained for the OS
        return coreCount - 2;
    }
}