Skip to content

Commit 5829e15

Browse files
Merge pull request #2580 from akarnokd/ComputationSchedulerConfig
Allow configuring the maximum number of computation scheduler threads
2 parents 0abfb74 + d983d2b commit 5829e15

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/main/java/rx/schedulers/EventLoopsScheduler.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,24 @@
3131
/** Manages a fixed number of workers. */
3232
private static final String THREAD_NAME_PREFIX = "RxComputationThreadPool-";
3333
private static final RxThreadFactory THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX);
34-
34+
/**
35+
* Key to setting the maximum number of computation scheduler threads.
36+
* Zero or less is interpreted as use available. Capped by available.
37+
*/
38+
static final String KEY_MAX_THREADS = "rx.scheduler.max-computation-threads";
39+
/** The maximum number of computation scheduler threads. */
40+
static final int MAX_THREADS;
41+
static {
42+
int maxThreads = Integer.getInteger(KEY_MAX_THREADS, 0);
43+
int ncpu = Runtime.getRuntime().availableProcessors();
44+
int max;
45+
if (maxThreads <= 0 || maxThreads > ncpu) {
46+
max = ncpu;
47+
} else {
48+
max = maxThreads;
49+
}
50+
MAX_THREADS = max;
51+
}
3552
static final class FixedSchedulerPool {
3653
final int cores;
3754

@@ -40,7 +57,7 @@ static final class FixedSchedulerPool {
4057

4158
FixedSchedulerPool() {
4259
// initialize event loops
43-
this.cores = Runtime.getRuntime().availableProcessors();
60+
this.cores = MAX_THREADS;
4461
this.eventLoops = new PoolWorker[cores];
4562
for (int i = 0; i < cores; i++) {
4663
this.eventLoops[i] = new PoolWorker(THREAD_FACTORY);

0 commit comments

Comments
 (0)