-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x: Add scheduler creation factories #5002
Changes from 1 commit
2ae2414
262f669
681b977
8b4d461
8009333
66111c5
a1029b4
e5c4a9e
90ae664
6a08b92
ea1ec6c
00af140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,20 +12,22 @@ | |
*/ | ||
package io.reactivex.internal.schedulers; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import io.reactivex.Scheduler; | ||
import io.reactivex.disposables.*; | ||
import io.reactivex.internal.disposables.EmptyDisposable; | ||
import io.reactivex.internal.functions.ObjectHelper; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* A scheduler with a shared, single threaded underlying ScheduledExecutorService. | ||
* @since 2.0 | ||
*/ | ||
public final class SingleScheduler extends Scheduler { | ||
|
||
final ThreadFactory threadFactory; | ||
final AtomicReference<ScheduledExecutorService> executor = new AtomicReference<ScheduledExecutorService>(); | ||
|
||
/** The name of the system property for setting the thread priority for this Scheduler. */ | ||
|
@@ -47,11 +49,20 @@ public final class SingleScheduler extends Scheduler { | |
} | ||
|
||
public SingleScheduler() { | ||
executor.lazySet(createExecutor()); | ||
this(SINGLE_THREAD_FACTORY); | ||
} | ||
|
||
/** | ||
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any | ||
* system properties for configuring new thread creation. Cannot be null. | ||
*/ | ||
public SingleScheduler(ThreadFactory threadFactory) { | ||
this.threadFactory = ObjectHelper.requireNonNull(threadFactory, "threadFactory was null"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either this is unnecessary or you forgot to do this with the other new constructors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, meant to remove it from all these constructors and just do it in the factory methods, accidentally missed this one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
executor.lazySet(createExecutor(threadFactory)); | ||
} | ||
|
||
static ScheduledExecutorService createExecutor() { | ||
return SchedulerPoolFactory.create(SINGLE_THREAD_FACTORY); | ||
static ScheduledExecutorService createExecutor(ThreadFactory threadFactory) { | ||
return SchedulerPoolFactory.create(threadFactory); | ||
} | ||
|
||
@Override | ||
|
@@ -66,7 +77,7 @@ public void start() { | |
return; | ||
} | ||
if (next == null) { | ||
next = createExecutor(); | ||
next = createExecutor(threadFactory); | ||
} | ||
if (executor.compareAndSet(current, next)) { | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,14 @@ | |
|
||
package io.reactivex.schedulers; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Executor; | ||
|
||
import io.reactivex.Scheduler; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.internal.functions.ObjectHelper; | ||
import io.reactivex.internal.schedulers.*; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Static factory methods for returning standard Scheduler instances. | ||
* <p> | ||
|
@@ -58,7 +59,7 @@ static final class IoHolder { | |
} | ||
|
||
static final class NewThreadHolder { | ||
static final Scheduler DEFAULT = NewThreadScheduler.instance(); | ||
static final Scheduler DEFAULT = new NewThreadScheduler(); | ||
} | ||
|
||
static { | ||
|
@@ -179,6 +180,90 @@ public static Scheduler from(Executor executor) { | |
return new ExecutorScheduler(executor); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#computation()}. | ||
* @return the created Scheduler instance | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@Experimental | ||
public static Scheduler newComputation() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we'd want to allow non-parameterized instances to be created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you expand on that a bit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the use case for creating yet another computation/io/newThread scheduler with the default settings? I thought you wanted to override the |
||
return new ComputationScheduler(); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#computation()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need some rewording emphasizing that the there is always a new Scheduler instance, unlike with the non-new method call. |
||
* except using {@code threadFactory} for thread creation. | ||
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any | ||
* system properties for configuring new thread creation. Cannot be null. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newComputation(ThreadFactory threadFactory) { | ||
return new ComputationScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory == null")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The usual NPE message is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, will update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#io()}. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newIo() { | ||
return new IoScheduler(); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#io()} | ||
* except using {@code threadFactory} for thread creation. | ||
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any | ||
* system properties for configuring new thread creation. Cannot be null. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newIo(ThreadFactory threadFactory) { | ||
return new IoScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory == null")); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#newThread()}. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newNewThread() { | ||
return new NewThreadScheduler(); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#newThread()} | ||
* except using {@code threadFactory} for thread creation. | ||
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any | ||
* system properties for configuring new thread creation. Cannot be null. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newNewThread(ThreadFactory threadFactory) { | ||
return new NewThreadScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory == null")); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()}. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newSingle() { | ||
return new SingleScheduler(); | ||
} | ||
|
||
/** | ||
* Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} | ||
* except using {@code threadFactory} for thread creation. | ||
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any | ||
* system properties for configuring new thread creation. Cannot be null. | ||
* @return the created Scheduler instance | ||
*/ | ||
@Experimental | ||
public static Scheduler newSingle(ThreadFactory threadFactory) { | ||
return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory == null")); | ||
} | ||
|
||
/** | ||
* Shuts down those standard Schedulers which support the SchedulerLifecycle interface. | ||
* <p>The operation is idempotent and thread-safe. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason why this was moved to the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It requires WORKER_THREAD_FACTORY to be initialized now since it's a parameter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.