Skip to content

Commit

Permalink
Merge pull request #287 from ReactiveX/jw/cached-instance
Browse files Browse the repository at this point in the history
Fix AndroidSchedulers to create an instance using hook only once.
  • Loading branch information
JakeWharton committed May 2, 2016
2 parents 3bc8aee + ce9c29a commit a45bcb6
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@
import android.os.Looper;
import rx.Scheduler;
import rx.android.plugins.RxAndroidPlugins;
import rx.android.plugins.RxAndroidSchedulersHook;

/** Android-specific Schedulers. */
public final class AndroidSchedulers {
private static final AndroidSchedulers INSTANCE = new AndroidSchedulers();

private final Scheduler mainThreadScheduler;

private AndroidSchedulers() {
throw new AssertionError("No instances");
}
RxAndroidSchedulersHook hook = RxAndroidPlugins.getInstance().getSchedulersHook();

// See https://github.com/ReactiveX/RxAndroid/issues/238
// https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
private static class MainThreadSchedulerHolder {
static final Scheduler MAIN_THREAD_SCHEDULER = new LooperScheduler(Looper.getMainLooper());
Scheduler main = hook.getMainThreadScheduler();
if (main != null) {
mainThreadScheduler = main;
} else {
mainThreadScheduler = new LooperScheduler(Looper.getMainLooper());
}
}

/** A {@link Scheduler} which executes actions on the Android UI thread. */
public static Scheduler mainThread() {
Scheduler scheduler =
RxAndroidPlugins.getInstance().getSchedulersHook().getMainThreadScheduler();
return scheduler != null ? scheduler : MainThreadSchedulerHolder.MAIN_THREAD_SCHEDULER;
return INSTANCE.mainThreadScheduler;
}

/** A {@link Scheduler} which executes actions on {@code looper}. */
Expand Down

0 comments on commit a45bcb6

Please sign in to comment.