Skip to content

Commit 5b0dd58

Browse files
authored
3.x: Add RxJavaPlugins.createExecutorScheduler (#7306)
* 3.x: Add RxJavaPlugins.createExecutorScheduler * Fix newline
1 parent e24e725 commit 5b0dd58

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import java.lang.Thread.UncaughtExceptionHandler;
1717
import java.util.Objects;
18-
import java.util.concurrent.ThreadFactory;
18+
import java.util.concurrent.*;
1919

2020
import org.reactivestreams.Subscriber;
2121

@@ -1302,6 +1302,26 @@ public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFacto
13021302
return new SingleScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null"));
13031303
}
13041304

1305+
/**
1306+
* Create an instance of a {@link Scheduler} by wrapping an existing {@link Executor}.
1307+
* <p>
1308+
* This method allows creating an {@code Executor}-backed {@code Scheduler} before the {@link Schedulers} class
1309+
* would initialize the standard {@code Scheduler}s.
1310+
*
1311+
* @param executor the {@code Executor} to wrap and turn into a {@code Scheduler}.
1312+
* @param interruptibleWorker if {@code true}, the tasks submitted to the {@link io.reactivex.rxjava3.core.Scheduler.Worker Scheduler.Worker} will
1313+
* be interrupted when the task is disposed.
1314+
* @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
1315+
* in a FIFO and non-overlapping manner, but allows interleaving with other tasks submitted to the underlying {@code Executor}.
1316+
* If {@code false}, the underlying FIFO scheme will execute as many tasks as it can before giving up the underlying {@code Executor} thread.
1317+
* @return the new {@code Scheduler} wrapping the {@code Executor}
1318+
* @since 3.1.0
1319+
*/
1320+
@NonNull
1321+
public static Scheduler createExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) {
1322+
return new ExecutorScheduler(executor, interruptibleWorker, fair);
1323+
}
1324+
13051325
/**
13061326
* Wraps the call to the function in try-catch and propagates thrown
13071327
* checked exceptions as RuntimeException.

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,18 @@ public static Scheduler single() {
378378
* execute those tasks "unexpectedly".
379379
* <p>
380380
* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance.
381+
* <p>
382+
* It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the
383+
* standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method
384+
* before the {@code Schedulers} class itself is accessed.
381385
* @param executor
382386
* the executor to wrap
383387
* @return the new {@code Scheduler} wrapping the {@code Executor}
384388
* @see #from(Executor, boolean, boolean)
385389
*/
386390
@NonNull
387391
public static Scheduler from(@NonNull Executor executor) {
388-
return new ExecutorScheduler(executor, false, false);
392+
return from(executor, false, false);
389393
}
390394

391395
/**
@@ -452,6 +456,10 @@ public static Scheduler from(@NonNull Executor executor) {
452456
* execute those tasks "unexpectedly".
453457
* <p>
454458
* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance.
459+
* <p>
460+
* It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the
461+
* standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method
462+
* before the {@code Schedulers} class itself is accessed.
455463
* <p>History: 2.2.6 - experimental
456464
* @param executor
457465
* the executor to wrap
@@ -463,7 +471,7 @@ public static Scheduler from(@NonNull Executor executor) {
463471
*/
464472
@NonNull
465473
public static Scheduler from(@NonNull Executor executor, boolean interruptibleWorker) {
466-
return new ExecutorScheduler(executor, interruptibleWorker, false);
474+
return from(executor, interruptibleWorker, false);
467475
}
468476

469477
/**
@@ -532,6 +540,11 @@ public static Scheduler from(@NonNull Executor executor, boolean interruptibleWo
532540
* execute those tasks "unexpectedly".
533541
* <p>
534542
* Note that this method returns a new {@code Scheduler} instance, even for the same {@code Executor} instance.
543+
* <p>
544+
* It is possible to wrap an {@code Executor} into a {@code Scheduler} without triggering the initialization of all the
545+
* standard schedulers by using the {@link RxJavaPlugins#createExecutorScheduler(Executor, boolean, boolean)} method
546+
* before the {@code Schedulers} class itself is accessed.
547+
*
535548
* @param executor
536549
* the executor to wrap
537550
* @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
544557
*/
545558
@NonNull
546559
public static Scheduler from(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) {
547-
return new ExecutorScheduler(executor, interruptibleWorker, fair);
560+
return RxJavaPlugins.createExecutorScheduler(executor, interruptibleWorker, fair);
548561
}
549562

550563
/**

0 commit comments

Comments
 (0)