Skip to content

Commit 151de82

Browse files
committed
adjust thread pool core thread size
1 parent fcb047a commit 151de82

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

taskscheduler/src/main/java/com/silencedut/taskscheduler/TaskScheduler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ public void error(String error) {
4949
};
5050

5151
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
52-
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
52+
/**
53+
* copy from AsyncTask
54+
* We want at least 2 threads and at most 4 threads in the core pool,
55+
* preferring to have 1 less than the CPU count to avoid saturating
56+
* the CPU with background work
57+
*/
58+
private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
59+
private static final int MAXIMUM_POOL_SIZE = CORE_POOL_SIZE * 2 + 1;
5360
private static final long KEEP_ALIVE = 60L;
5461
private static final BlockingQueue<Runnable> POOL_WORK_QUEUE =
5562
new LinkedBlockingQueue<>(128);
@@ -67,6 +74,7 @@ private static TaskScheduler getInstance() {
6774

6875
private TaskScheduler() {
6976

77+
7078
mParallelExecutor = new ThreadPoolExecutor(CPU_COUNT,MAXIMUM_POOL_SIZE,
7179
KEEP_ALIVE,TimeUnit.SECONDS,POOL_WORK_QUEUE,ThreadFactory.TASKSCHEDULER_FACTORY);
7280

0 commit comments

Comments
 (0)