Package rx.internal.schedulers
Class NewThreadWorker
- java.lang.Object
-
- rx.Scheduler.Worker
-
- rx.internal.schedulers.NewThreadWorker
-
- All Implemented Interfaces:
Subscription
- Direct Known Subclasses:
CachedThreadScheduler.ThreadWorker
,EventLoopsScheduler.PoolWorker
public class NewThreadWorker extends Scheduler.Worker implements Subscription
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.Object
cachedSetRemoveOnCancelPolicyMethod
Improves performance oftryEnableCancelPolicy(ScheduledExecutorService)
.private java.util.concurrent.ScheduledExecutorService
executor
private static java.util.concurrent.ConcurrentHashMap<java.util.concurrent.ScheduledThreadPoolExecutor,java.util.concurrent.ScheduledThreadPoolExecutor>
EXECUTORS
private static java.lang.String
FREQUENCY_KEY
The purge frequency in milliseconds.(package private) boolean
isUnsubscribed
private static java.util.concurrent.atomic.AtomicReference<java.util.concurrent.ScheduledExecutorService>
PURGE
private static java.lang.String
PURGE_FORCE_KEY
Force the use of purge (true/false).static int
PURGE_FREQUENCY
The purge frequency in milliseconds.private static java.lang.String
PURGE_THREAD_PREFIX
private static java.lang.Object
SET_REMOVE_ON_CANCEL_POLICY_METHOD_NOT_SUPPORTED
Possible value ofcachedSetRemoveOnCancelPolicyMethod
which means that cancel policy is not supported.private static boolean
SHOULD_TRY_ENABLE_CANCEL_POLICY
-
Constructor Summary
Constructors Constructor Description NewThreadWorker(java.util.concurrent.ThreadFactory threadFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
deregisterExecutor(java.util.concurrent.ScheduledExecutorService service)
Deregisters the executor service.(package private) static java.lang.reflect.Method
findSetRemoveOnCancelPolicyMethod(java.util.concurrent.ScheduledExecutorService executor)
Tries to find"setRemoveOnCancelPolicy(boolean)"
method in the class of passed executor.boolean
isUnsubscribed()
Indicates whether thisSubscription
is currently unsubscribed.(package private) static void
purgeExecutors()
Purges each registered executor and eagerly evicts shutdown executors.static void
registerExecutor(java.util.concurrent.ScheduledThreadPoolExecutor service)
Registers the given executor service and starts the purge thread if not already started.Subscription
schedule(Action0 action)
Schedules an Action for execution.Subscription
schedule(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit)
Schedules an Action for execution at some point in the future.ScheduledAction
scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit)
Schedules the given action by wrapping it into a ScheduledAction on the underlying ExecutorService, returning the ScheduledAction.ScheduledAction
scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit, SubscriptionList parent)
ScheduledAction
scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit, CompositeSubscription parent)
static boolean
tryEnableCancelPolicy(java.util.concurrent.ScheduledExecutorService executor)
Tries to enable the Java 7+ setRemoveOnCancelPolicy.void
unsubscribe()
Stops the receipt of notifications on theSubscriber
that was registered when this Subscription was received.-
Methods inherited from class rx.Scheduler.Worker
now, schedulePeriodically
-
-
-
-
Field Detail
-
executor
private final java.util.concurrent.ScheduledExecutorService executor
-
isUnsubscribed
volatile boolean isUnsubscribed
-
FREQUENCY_KEY
private static final java.lang.String FREQUENCY_KEY
The purge frequency in milliseconds.- See Also:
- Constant Field Values
-
PURGE_FORCE_KEY
private static final java.lang.String PURGE_FORCE_KEY
Force the use of purge (true/false).- See Also:
- Constant Field Values
-
PURGE_THREAD_PREFIX
private static final java.lang.String PURGE_THREAD_PREFIX
- See Also:
- Constant Field Values
-
SHOULD_TRY_ENABLE_CANCEL_POLICY
private static final boolean SHOULD_TRY_ENABLE_CANCEL_POLICY
-
PURGE_FREQUENCY
public static final int PURGE_FREQUENCY
The purge frequency in milliseconds.
-
EXECUTORS
private static final java.util.concurrent.ConcurrentHashMap<java.util.concurrent.ScheduledThreadPoolExecutor,java.util.concurrent.ScheduledThreadPoolExecutor> EXECUTORS
-
PURGE
private static final java.util.concurrent.atomic.AtomicReference<java.util.concurrent.ScheduledExecutorService> PURGE
-
cachedSetRemoveOnCancelPolicyMethod
private static volatile java.lang.Object cachedSetRemoveOnCancelPolicyMethod
Improves performance oftryEnableCancelPolicy(ScheduledExecutorService)
. Also, it works even for inheritance:Method
of base class can be invoked on the instance of child class.
-
SET_REMOVE_ON_CANCEL_POLICY_METHOD_NOT_SUPPORTED
private static final java.lang.Object SET_REMOVE_ON_CANCEL_POLICY_METHOD_NOT_SUPPORTED
Possible value ofcachedSetRemoveOnCancelPolicyMethod
which means that cancel policy is not supported.
-
-
Method Detail
-
registerExecutor
public static void registerExecutor(java.util.concurrent.ScheduledThreadPoolExecutor service)
Registers the given executor service and starts the purge thread if not already started.public
visibility reason: called from other package(s) within RxJava- Parameters:
service
- a scheduled thread pool executor instance
-
deregisterExecutor
public static void deregisterExecutor(java.util.concurrent.ScheduledExecutorService service)
Deregisters the executor service.public
visibility reason: called from other package(s) within RxJava- Parameters:
service
- a scheduled thread pool executor instance
-
purgeExecutors
@SuppressAnimalSniffer static void purgeExecutors()
Purges each registered executor and eagerly evicts shutdown executors.
-
tryEnableCancelPolicy
public static boolean tryEnableCancelPolicy(java.util.concurrent.ScheduledExecutorService executor)
Tries to enable the Java 7+ setRemoveOnCancelPolicy.public
visibility reason: called from other package(s) within RxJava. If the method returns false, theregisterExecutor(ScheduledThreadPoolExecutor)
may be called to enable the backup option of purging the executors.- Parameters:
executor
- the executor to call setRemoveOnCaneclPolicy if available.- Returns:
- true if the policy was successfully enabled
-
findSetRemoveOnCancelPolicyMethod
static java.lang.reflect.Method findSetRemoveOnCancelPolicyMethod(java.util.concurrent.ScheduledExecutorService executor)
Tries to find"setRemoveOnCancelPolicy(boolean)"
method in the class of passed executor.- Parameters:
executor
- whose class will be used to search for required method.- Returns:
"setRemoveOnCancelPolicy(boolean)"
Method
ornull
if requiredMethod
was not found.
-
schedule
public Subscription schedule(Action0 action)
Description copied from class:Scheduler.Worker
Schedules an Action for execution.- Specified by:
schedule
in classScheduler.Worker
- Parameters:
action
- Action to schedule- Returns:
- a subscription to be able to unsubscribe the action (unschedule it if not executed)
-
schedule
public Subscription schedule(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit)
Description copied from class:Scheduler.Worker
Schedules an Action for execution at some point in the future.Note to implementors: non-positive
delayTime
should be regarded as undelayed schedule, i.e., as if theScheduler.Worker.schedule(rx.functions.Action0)
was called.- Specified by:
schedule
in classScheduler.Worker
- Parameters:
action
- the Action to scheduledelayTime
- time to wait before executing the action; non-positive values indicate an undelayed scheduleunit
- the time unit ofdelayTime
- Returns:
- a subscription to be able to unsubscribe the action (unschedule it if not executed)
-
scheduleActual
public ScheduledAction scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit)
Schedules the given action by wrapping it into a ScheduledAction on the underlying ExecutorService, returning the ScheduledAction.- Parameters:
action
- the action to wrap and scheduledelayTime
- the delay in executionunit
- the time unit of the delay- Returns:
- the wrapper ScheduledAction
-
scheduleActual
public ScheduledAction scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit, CompositeSubscription parent)
-
scheduleActual
public ScheduledAction scheduleActual(Action0 action, long delayTime, java.util.concurrent.TimeUnit unit, SubscriptionList parent)
-
unsubscribe
public void unsubscribe()
Description copied from interface:Subscription
Stops the receipt of notifications on theSubscriber
that was registered when this Subscription was received.This allows unregistering an
Subscriber
before it has finished receiving all events (i.e. before onCompleted is called).- Specified by:
unsubscribe
in interfaceSubscription
-
isUnsubscribed
public boolean isUnsubscribed()
Description copied from interface:Subscription
Indicates whether thisSubscription
is currently unsubscribed.- Specified by:
isUnsubscribed
in interfaceSubscription
- Returns:
true
if thisSubscription
is currently unsubscribed,false
otherwise
-
-