Skip to content

Commit 3330943

Browse files
authored
Removes the purge thread in favor of standard ScheduledThreadPoolExecutor APIs (#7293)
The RXSchedulerPurge thread is currently enabled by default and runs every second to call purge() on each executor in the pool. This is causing significant issues for low powered devices (e.g. mobile phones), because it needs to periodically wake up the CPU to perform purging. The RXSchedulerPurge thread could be completely removed in favor of using the standard setRemoveOnCancelPolicy() API on the ScheduledThreadPoolExecutor which became available in Java 7 and offers removal of cancelled tasks at the moment they are cancelled in O(1).
1 parent c5883dc commit 3330943

File tree

4 files changed

+4
-188
lines changed

4 files changed

+4
-188
lines changed

src/main/java/io/reactivex/rxjava3/internal/schedulers/SchedulerPoolFactory.java

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -31,86 +31,11 @@ private SchedulerPoolFactory() {
3131

3232
static final String PURGE_ENABLED_KEY = "rx3.purge-enabled";
3333

34-
/**
35-
* Indicates the periodic purging of the ScheduledExecutorService is enabled.
36-
*/
3734
public static final boolean PURGE_ENABLED;
3835

39-
static final String PURGE_PERIOD_SECONDS_KEY = "rx3.purge-period-seconds";
40-
41-
/**
42-
* Indicates the purge period of the ScheduledExecutorServices created by create().
43-
*/
44-
public static final int PURGE_PERIOD_SECONDS;
45-
46-
static final AtomicReference<ScheduledExecutorService> PURGE_THREAD =
47-
new AtomicReference<>();
48-
49-
// Upcast to the Map interface here to avoid 8.x compatibility issues.
50-
// See http://stackoverflow.com/a/32955708/61158
51-
static final Map<ScheduledThreadPoolExecutor, Object> POOLS =
52-
new ConcurrentHashMap<>();
53-
54-
/**
55-
* Starts the purge thread if not already started.
56-
*/
57-
public static void start() {
58-
tryStart(PURGE_ENABLED);
59-
}
60-
61-
static void tryStart(boolean purgeEnabled) {
62-
if (purgeEnabled) {
63-
for (;;) {
64-
ScheduledExecutorService curr = PURGE_THREAD.get();
65-
if (curr != null) {
66-
return;
67-
}
68-
ScheduledExecutorService next = Executors.newScheduledThreadPool(1, new RxThreadFactory("RxSchedulerPurge"));
69-
if (PURGE_THREAD.compareAndSet(curr, next)) {
70-
71-
next.scheduleAtFixedRate(new ScheduledTask(), PURGE_PERIOD_SECONDS, PURGE_PERIOD_SECONDS, TimeUnit.SECONDS);
72-
73-
return;
74-
} else {
75-
next.shutdownNow();
76-
}
77-
}
78-
}
79-
}
80-
81-
/**
82-
* Stops the purge thread.
83-
*/
84-
public static void shutdown() {
85-
ScheduledExecutorService exec = PURGE_THREAD.getAndSet(null);
86-
if (exec != null) {
87-
exec.shutdownNow();
88-
}
89-
POOLS.clear();
90-
}
91-
9236
static {
9337
SystemPropertyAccessor propertyAccessor = new SystemPropertyAccessor();
9438
PURGE_ENABLED = getBooleanProperty(true, PURGE_ENABLED_KEY, true, true, propertyAccessor);
95-
PURGE_PERIOD_SECONDS = getIntProperty(PURGE_ENABLED, PURGE_PERIOD_SECONDS_KEY, 1, 1, propertyAccessor);
96-
97-
start();
98-
}
99-
100-
static int getIntProperty(boolean enabled, String key, int defaultNotFound, int defaultNotEnabled, Function<String, String> propertyAccessor) {
101-
if (enabled) {
102-
try {
103-
String value = propertyAccessor.apply(key);
104-
if (value == null) {
105-
return defaultNotFound;
106-
}
107-
return Integer.parseInt(value);
108-
} catch (Throwable ex) {
109-
Exceptions.throwIfFatal(ex);
110-
return defaultNotFound;
111-
}
112-
}
113-
return defaultNotEnabled;
11439
}
11540

11641
static boolean getBooleanProperty(boolean enabled, String key, boolean defaultNotFound, boolean defaultNotEnabled, Function<String, String> propertyAccessor) {
@@ -142,28 +67,8 @@ public String apply(String t) {
14267
* @return the ScheduledExecutorService
14368
*/
14469
public static ScheduledExecutorService create(ThreadFactory factory) {
145-
final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1, factory);
146-
tryPutIntoPool(PURGE_ENABLED, exec);
70+
final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1, factory);
71+
exec.setRemoveOnCancelPolicy(PURGE_ENABLED);
14772
return exec;
14873
}
149-
150-
static void tryPutIntoPool(boolean purgeEnabled, ScheduledExecutorService exec) {
151-
if (purgeEnabled && exec instanceof ScheduledThreadPoolExecutor) {
152-
ScheduledThreadPoolExecutor e = (ScheduledThreadPoolExecutor) exec;
153-
POOLS.put(e, exec);
154-
}
155-
}
156-
157-
static final class ScheduledTask implements Runnable {
158-
@Override
159-
public void run() {
160-
for (ScheduledThreadPoolExecutor e : new ArrayList<>(POOLS.keySet())) {
161-
if (e.isShutdown()) {
162-
POOLS.remove(e);
163-
} else {
164-
e.purge();
165-
}
166-
}
167-
}
168-
}
16974
}

src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
* <li>{@code rx3.computation-priority} (int): sets the thread priority of the {@link #computation()} {@code Scheduler}, default is {@link Thread#NORM_PRIORITY}</li>
3939
* <li>{@code rx3.newthread-priority} (int): sets the thread priority of the {@link #newThread()} {@code Scheduler}, default is {@link Thread#NORM_PRIORITY}</li>
4040
* <li>{@code rx3.single-priority} (int): sets the thread priority of the {@link #single()} {@code Scheduler}, default is {@link Thread#NORM_PRIORITY}</li>
41-
* <li>{@code rx3.purge-enabled} (boolean): enables periodic purging of all {@code Scheduler}'s backing thread pools, default is {@code false}</li>
42-
* <li>{@code rx3.purge-period-seconds} (int): specifies the periodic purge interval of all {@code Scheduler}'s backing thread pools, default is 1 second</li>
41+
* <li>{@code rx3.purge-enabled} (boolean): enables purging of all {@code Scheduler}'s backing thread pools, default is {@code true}</li>
4342
* <li>{@code rx3.scheduler.use-nanotime} (boolean): {@code true} instructs {@code Scheduler} to use {@link System#nanoTime()} for {@link Scheduler#now(TimeUnit)},
4443
* instead of default {@link System#currentTimeMillis()} ({@code false})</li>
4544
* </ul>
@@ -556,7 +555,6 @@ public static void shutdown() {
556555
newThread().shutdown();
557556
single().shutdown();
558557
trampoline().shutdown();
559-
SchedulerPoolFactory.shutdown();
560558
}
561559

562560
/**
@@ -569,7 +567,6 @@ public static void start() {
569567
newThread().start();
570568
single().start();
571569
trampoline().start();
572-
SchedulerPoolFactory.start();
573570
}
574571

575572
static final class IOTask implements Supplier<Scheduler> {

src/test/java/io/reactivex/rxjava3/internal/schedulers/SchedulerPoolFactoryTest.java

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,6 @@ public void utilityClass() {
3030
TestHelper.checkUtilityClass(SchedulerPoolFactory.class);
3131
}
3232

33-
@Test
34-
public void multiStartStop() {
35-
SchedulerPoolFactory.shutdown();
36-
37-
SchedulerPoolFactory.shutdown();
38-
39-
SchedulerPoolFactory.tryStart(false);
40-
41-
assertNull(SchedulerPoolFactory.PURGE_THREAD.get());
42-
43-
SchedulerPoolFactory.start();
44-
45-
// restart schedulers
46-
Schedulers.shutdown();
47-
48-
Schedulers.start();
49-
}
50-
51-
@Test
52-
public void startRace() throws InterruptedException {
53-
try {
54-
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
55-
SchedulerPoolFactory.shutdown();
56-
57-
Runnable r1 = new Runnable() {
58-
@Override
59-
public void run() {
60-
SchedulerPoolFactory.start();
61-
}
62-
};
63-
64-
TestHelper.race(r1, r1);
65-
}
66-
67-
} finally {
68-
// restart schedulers
69-
Schedulers.shutdown();
70-
71-
Thread.sleep(200);
72-
73-
Schedulers.start();
74-
}
75-
}
76-
7733
@Test
7834
public void boolPropertiesDisabledReturnsDefaultDisabled() throws Throwable {
7935
assertTrue(SchedulerPoolFactory.getBooleanProperty(false, "key", false, true, failingPropertiesAccessor));
@@ -98,30 +54,6 @@ public void boolPropertiesReturnsValue() throws Throwable {
9854
assertFalse(SchedulerPoolFactory.getBooleanProperty(true, "false", false, true, Functions.<String>identity()));
9955
}
10056

101-
@Test
102-
public void intPropertiesDisabledReturnsDefaultDisabled() throws Throwable {
103-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(false, "key", 0, -1, failingPropertiesAccessor));
104-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(false, "key", 1, -1, failingPropertiesAccessor));
105-
}
106-
107-
@Test
108-
public void intPropertiesEnabledMissingReturnsDefaultMissing() throws Throwable {
109-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 0, missingPropertiesAccessor));
110-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 1, missingPropertiesAccessor));
111-
}
112-
113-
@Test
114-
public void intPropertiesFailureReturnsDefaultMissing() throws Throwable {
115-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 0, failingPropertiesAccessor));
116-
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 1, failingPropertiesAccessor));
117-
}
118-
119-
@Test
120-
public void intPropertiesReturnsValue() throws Throwable {
121-
assertEquals(1, SchedulerPoolFactory.getIntProperty(true, "1", 0, 4, Functions.<String>identity()));
122-
assertEquals(2, SchedulerPoolFactory.getIntProperty(true, "2", 3, 5, Functions.<String>identity()));
123-
}
124-
12557
static final Function<String, String> failingPropertiesAccessor = new Function<String, String>() {
12658
@Override
12759
public String apply(String v) throws Throwable {
@@ -135,22 +67,4 @@ public String apply(String v) throws Throwable {
13567
return null;
13668
}
13769
};
138-
139-
@Test
140-
public void putIntoPoolNoPurge() {
141-
int s = SchedulerPoolFactory.POOLS.size();
142-
143-
SchedulerPoolFactory.tryPutIntoPool(false, null);
144-
145-
assertEquals(s, SchedulerPoolFactory.POOLS.size());
146-
}
147-
148-
@Test
149-
public void putIntoPoolNonThreadPool() {
150-
int s = SchedulerPoolFactory.POOLS.size();
151-
152-
SchedulerPoolFactory.tryPutIntoPool(true, null);
153-
154-
assertEquals(s, SchedulerPoolFactory.POOLS.size());
155-
}
15670
}

src/test/java/io/reactivex/rxjava3/schedulers/ExecutorSchedulerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void run() {
9393

9494
System.out.println("Wait before second GC");
9595
System.out.println("JDK 6 purge is N log N because it removes and shifts one by one");
96-
int t = (int)(n * Math.log(n) / 100) + SchedulerPoolFactory.PURGE_PERIOD_SECONDS * 1000;
96+
int t = (int)(n * Math.log(n) / 100) + 1000;
9797
int sleepStep = 100;
9898
while (t > 0) {
9999
System.out.printf(" >> Waiting for purge: %.2f s remaining%n", t / 1000d);

0 commit comments

Comments
 (0)