Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x: fix reset() shutting down everything other than the schedulers #3996

Merged
merged 1 commit into from
Jun 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/main/java/rx/schedulers/Schedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static Schedulers getInstance() {
if (INSTANCE.compareAndSet(null, current)) {
return current;
} else {
shutdown();
current.shutdownInstance();
}
}
}
Expand Down Expand Up @@ -168,8 +168,10 @@ public static Scheduler from(Executor executor) {
*/
@Experimental
public static void reset() {
shutdown();
INSTANCE.set(null);
Schedulers s = INSTANCE.getAndSet(null);
if (s != null) {
s.shutdownInstance();
}
}

/**
Expand All @@ -178,16 +180,10 @@ public static void reset() {
*/
/* public test only */ static void start() {
Schedulers s = getInstance();

s.startInstance();

synchronized (s) {
if (s.computationScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.computationScheduler).start();
}
if (s.ioScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.ioScheduler).start();
}
if (s.newThreadScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.newThreadScheduler).start();
}
GenericScheduledExecutorService.INSTANCE.start();

RxRingBuffer.SPSC_POOL.start();
Expand All @@ -201,22 +197,44 @@ public static void reset() {
*/
public static void shutdown() {
Schedulers s = getInstance();
synchronized (s) {
if (s.computationScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.computationScheduler).shutdown();
}
if (s.ioScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.ioScheduler).shutdown();
}
if (s.newThreadScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) s.newThreadScheduler).shutdown();
}
s.shutdownInstance();

synchronized (s) {
GenericScheduledExecutorService.INSTANCE.shutdown();

RxRingBuffer.SPSC_POOL.shutdown();

RxRingBuffer.SPMC_POOL.shutdown();
}
}

/**
* Start the instance-specific schedulers.
*/
synchronized void startInstance() {
Copy link
Contributor

@ZacSweers ZacSweers Jun 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not make these private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid too much private modifiers; generally it adds hidden bridge methods, increasing the method count which is bad for Android. Also package level access leaves it open to same-package unit testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, though this case wouldn't generate synthetic accessors I don't think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, bridges are not in play here; it has become a second nature of me scoping as package-private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad approach :). LGTM then

if (computationScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) computationScheduler).start();
}
if (ioScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) ioScheduler).start();
}
if (newThreadScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) newThreadScheduler).start();
}
}

/**
* Start the instance-specific schedulers.
*/
synchronized void shutdownInstance() {
if (computationScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) computationScheduler).shutdown();
}
if (ioScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) ioScheduler).shutdown();
}
if (newThreadScheduler instanceof SchedulerLifecycle) {
((SchedulerLifecycle) newThreadScheduler).shutdown();
}
}
}