diff options
author | Keuin <[email protected]> | 2021-01-22 18:59:47 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-22 18:59:47 +0800 |
commit | 1c23fc14be8a0ac9542f1412448c4d896756ba01 (patch) | |
tree | 98a89f19f84877d24a99f9a256408e46b4c32f29 /src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java | |
parent | 2f1d2ec7ddaebbbd19cde6314afa873f6fb964f4 (diff) |
Speed up the incremental backup by using multiple CPU cores if available (use multiple threads to calculate the hash).
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java new file mode 100644 index 0000000..b270895 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java @@ -0,0 +1,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; + } +} |