From 8220d5809853c69cd2697a8e95258b4d8c9cbfab Mon Sep 17 00:00:00 2001 From: akarnokd Date: Mon, 2 Aug 2021 09:21:06 +0200 Subject: [PATCH 1/2] 3.x: Add RxJavaPlugins.createExecutorScheduler --- .../rxjava3/plugins/RxJavaPlugins.java | 21 ++++++++++++++++++- .../rxjava3/schedulers/Schedulers.java | 19 ++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java b/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java index 506fb65c67..5f93b84c40 100644 --- a/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java +++ b/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java @@ -15,7 +15,7 @@ import java.lang.Thread.UncaughtExceptionHandler; import java.util.Objects; -import java.util.concurrent.ThreadFactory; +import java.util.concurrent.*; import org.reactivestreams.Subscriber; @@ -1302,6 +1302,25 @@ public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFacto return new SingleScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null")); } + /** + * Create an instance of a {@link Scheduler} by wrapping an existing {@link Executor}. + *

+ * This method allows creating an {@code Executor}-backed {@code Scheduler} before the {@link Schedulers} class + * would initialize the standard {@code Scheduler}s. + * + * @param executor the {@code Executor} to wrap and turn into a {@code Scheduler}. + * @param interruptibleWorker if {@code true}, the tasks submitted to the {@link io.reactivex.rxjava3.core.Scheduler.Worker Scheduler.Worker} will + * be interrupted when the task is disposed. + * @param fair if {@code true}, tasks submitted to the {@code Scheduler} or {@code Worker} will be executed by the underlying {@code Executor} one after the other, still + * in a FIFO and non-overlapping manner, but allows interleaving with other tasks submitted to the underlying {@code Executor}. + * If {@code false}, the underlying FIFO scheme will execute as many tasks as it can before giving up the underlying {@code Executor} thread. + * @return the new {@code Scheduler} wrapping the {@code Executor} + * @since 3.1.0 + */ + @NonNull + public static Scheduler createExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) { + return new ExecutorScheduler(executor, interruptibleWorker, fair); + } /** * Wraps the call to the function in try-catch and propagates thrown * checked exceptions as RuntimeException. diff --git a/src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java b/src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java index cc7e693b3d..112c2559b0 100644 --- a/src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java +++ b/src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java @@ -378,6 +378,10 @@ public static Scheduler single() { * execute those tasks "unexpectedly". *

* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance. + *

+ * It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the + * standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method + * before the {@code Schedulers} class itself is accessed. * @param executor * the executor to wrap * @return the new {@code Scheduler} wrapping the {@code Executor} @@ -385,7 +389,7 @@ public static Scheduler single() { */ @NonNull public static Scheduler from(@NonNull Executor executor) { - return new ExecutorScheduler(executor, false, false); + return from(executor, false, false); } /** @@ -452,6 +456,10 @@ public static Scheduler from(@NonNull Executor executor) { * execute those tasks "unexpectedly". *

* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance. + *

+ * It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the + * standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method + * before the {@code Schedulers} class itself is accessed. *

History: 2.2.6 - experimental * @param executor * the executor to wrap @@ -463,7 +471,7 @@ public static Scheduler from(@NonNull Executor executor) { */ @NonNull public static Scheduler from(@NonNull Executor executor, boolean interruptibleWorker) { - return new ExecutorScheduler(executor, interruptibleWorker, false); + return from(executor, interruptibleWorker, false); } /** @@ -532,6 +540,11 @@ public static Scheduler from(@NonNull Executor executor, boolean interruptibleWo * execute those tasks "unexpectedly". *

* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance. + *

+ * It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the + * standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method + * before the {@code Schedulers} class itself is accessed. + * * @param executor * the executor to wrap * @param interruptibleWorker if {@code true}, the tasks submitted to the {@link io.reactivex.rxjava3.core.Scheduler.Worker Scheduler.Worker} will @@ -544,7 +557,7 @@ public static Scheduler from(@NonNull Executor executor, boolean interruptibleWo */ @NonNull public static Scheduler from(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) { - return new ExecutorScheduler(executor, interruptibleWorker, fair); + return RxJavaPlugins.createExecutorScheduler(executor, interruptibleWorker, fair); } /** From aff78341b1f0829c1c227e21e8ffc19941016318 Mon Sep 17 00:00:00 2001 From: akarnokd Date: Mon, 2 Aug 2021 09:23:15 +0200 Subject: [PATCH 2/2] Fix newline --- src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java b/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java index 5f93b84c40..c1115bb478 100644 --- a/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java +++ b/src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java @@ -1321,6 +1321,7 @@ public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFacto public static Scheduler createExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) { return new ExecutorScheduler(executor, interruptibleWorker, fair); } + /** * Wraps the call to the function in try-catch and propagates thrown * checked exceptions as RuntimeException.