-
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
Helper method to submit an (interruptible) action to an ExecutorService #2761
Changes from all commits
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 |
---|---|---|
|
@@ -15,10 +15,13 @@ | |
*/ | ||
package rx.schedulers; | ||
|
||
import rx.Scheduler; | ||
import rx.plugins.RxJavaPlugins; | ||
import java.util.concurrent.*; | ||
|
||
import java.util.concurrent.Executor; | ||
import rx.*; | ||
import rx.functions.Action0; | ||
import rx.internal.schedulers.ScheduledAction; | ||
import rx.plugins.RxJavaPlugins; | ||
import rx.subscriptions.CompositeSubscription; | ||
|
||
/** | ||
* Static factory methods for creating Schedulers. | ||
|
@@ -136,4 +139,52 @@ public static TestScheduler test() { | |
public static Scheduler from(Executor executor) { | ||
return new ExecutorScheduler(executor); | ||
} | ||
/** | ||
* Submit an Action0 to the specified executor service with the option to interrupt the task | ||
* on unsubscription and add it to a parent composite subscription. | ||
* @param executor the target executor service | ||
* @param action the action to execute | ||
* @param parent if not {@code null} the subscription representing the action is added to this composite with logic to remove it | ||
* once the action completes or is unsubscribed. | ||
* @param interruptOnUnsubscribe if {@code false}, unsubscribing the task will not interrupt the task if it is running | ||
* @return the Subscription representing the scheduled action which is also added to the {@code parent} composite | ||
*/ | ||
public static Subscription submitTo(ExecutorService executor, Action0 action, CompositeSubscription parent, boolean interruptOnUnsubscribe) { | ||
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. Looks good I think. Should we just us 'CompositeSubscription' or does this warrant an interface for subscriptions that support removal? 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. Certainly, an interface would be more general but I'm afraid it turns the usage places of CompositeSubscription into bimorphic or even megamorphic call sites. Let's leave it as CompositeSubscription for now. |
||
ScheduledAction sa = new ScheduledAction(action, interruptOnUnsubscribe); | ||
|
||
if (parent != null) { | ||
parent.add(sa); | ||
sa.addParent(parent); | ||
} | ||
|
||
Future<?> f = executor.submit(sa); | ||
sa.add(f); | ||
|
||
return sa; | ||
} | ||
/** | ||
* Submit an Action0 to the specified executor service with the given delay and the option to interrupt the task | ||
* on unsubscription and add it to a parent composite subscription. | ||
* @param executor the target executor service | ||
* @param action the action to execute | ||
* @param delay the delay value | ||
* @param unit the time unit of the delay value | ||
* @param parent if not {@code null} the subscription representing the action is added to this composite with logic to remove it | ||
* once the action completes or is unsubscribed. | ||
* @param interruptOnUnsubscribe if {@code false}, unsubscribing the task will not interrupt the task if it is running | ||
* @return the Subscription representing the scheduled action which is also added to the {@code parent} composite | ||
*/ | ||
public static Subscription submitTo(ScheduledExecutorService executor, Action0 action, long delay, TimeUnit unit, CompositeSubscription parent, boolean interruptOnUnsubscribe) { | ||
ScheduledAction sa = new ScheduledAction(action, interruptOnUnsubscribe); | ||
|
||
if (parent != null) { | ||
parent.add(sa); | ||
sa.addParent(parent); | ||
} | ||
|
||
Future<?> f = executor.schedule(sa, delay, unit); | ||
sa.add(f); | ||
|
||
return sa; | ||
} | ||
} |
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.
I'm not sure it is ever good for this to be a global default. Haven't we learned that it causes nasty issues in places like event loops? It seems only appropriate for separate threads, like the IO or NewThread schedulers.
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.
Should I add such parameters to the scheduler implementations directly or have them fixed?