ScheduledThreadPoolExecutor Class

Definition

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.

[Android.Runtime.Register("java/util/concurrent/ScheduledThreadPoolExecutor", DoNotGenerateAcw=true)]
public class ScheduledThreadPoolExecutor : Java.Util.Concurrent.ThreadPoolExecutor, IDisposable, Java.Interop.IJavaPeerable, Java.Util.Concurrent.IScheduledExecutorService
[<Android.Runtime.Register("java/util/concurrent/ScheduledThreadPoolExecutor", DoNotGenerateAcw=true)>]
type ScheduledThreadPoolExecutor = class
    inherit ThreadPoolExecutor
    interface IScheduledExecutorService
    interface IExecutorService
    interface IExecutor
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Attributes
Implements

Remarks

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to java.util.Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.

When a submitted task is cancelled before it is run, execution is suppressed. By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks.

Successive executions of a periodic task scheduled via #scheduleAtFixedRate scheduleAtFixedRate or #scheduleWithFixedDelay scheduleWithFixedDelay do not overlap. While different executions may be performed by different threads, the effects of prior executions <i>happen-before</i> those of subsequent ones.

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run.

As with ThreadPoolExecutor, if not otherwise specified, this class uses Executors#defaultThreadFactory as the default thread factory, and ThreadPoolExecutor.AbortPolicy as the default rejected execution handler.

<b>Extension notes:</b> This class overrides the ThreadPoolExecutor#execute(Runnable) execute and AbstractExecutorService#submit(Runnable) submit methods to generate internal ScheduledFuture objects to control per-task delays and scheduling. To preserve functionality, any further overrides of these methods in subclasses must invoke superclass versions, which effectively disables additional task customization. However, this class provides alternative protected extension method decorateTask (one version each for Runnable and Callable) that can be used to customize the concrete task types used to execute commands entered via execute, submit, schedule, scheduleAtFixedRate, and scheduleWithFixedDelay. By default, a ScheduledThreadPoolExecutor uses a task type extending FutureTask. However, this may be modified or replaced using subclasses of the form:

{@code
            public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {

              static class CustomTask<V> implements RunnableScheduledFuture<V> { ... }

              protected <V> RunnableScheduledFuture<V> decorateTask(
                           Runnable r, RunnableScheduledFuture<V> task) {
                  return new CustomTask<V>(r, task);
              }

              protected <V> RunnableScheduledFuture<V> decorateTask(
                           Callable<V> c, RunnableScheduledFuture<V> task) {
                  return new CustomTask<V>(c, task);
              }
              // ... add constructors, etc.
            }}

Added in 1.5.

Java documentation for java.util.concurrent.ScheduledThreadPoolExecutor.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ScheduledThreadPoolExecutor(Int32)

Creates a new ScheduledThreadPoolExecutor with the given core pool size.

ScheduledThreadPoolExecutor(Int32, IRejectedExecutionHandler)

Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

ScheduledThreadPoolExecutor(Int32, IThreadFactory)

Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

ScheduledThreadPoolExecutor(Int32, IThreadFactory, IRejectedExecutionHandler)

Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

ScheduledThreadPoolExecutor(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

ActiveCount

Returns the approximate number of threads that are actively executing tasks.

(Inherited from ThreadPoolExecutor)
Class

Returns the runtime class of this Object.

(Inherited from Object)
CompletedTaskCount

Returns the approximate total number of tasks that have completed execution.

(Inherited from ThreadPoolExecutor)
ContinueExistingPeriodicTasksAfterShutdownPolicy

Gets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown. -or- Sets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown.

CorePoolSize

Returns the core number of threads. -or- Sets the core number of threads.

(Inherited from ThreadPoolExecutor)
ExecuteExistingDelayedTasksAfterShutdownPolicy

Gets the policy on whether to execute existing delayed tasks even when this executor has been shutdown. -or- Sets the policy on whether to execute existing delayed tasks even when this executor has been shutdown.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsShutdown (Inherited from ThreadPoolExecutor)
IsTerminated (Inherited from ThreadPoolExecutor)
IsTerminating

Returns true if this executor is in the process of terminating after #shutdown or #shutdownNow but has not completely terminated.

(Inherited from ThreadPoolExecutor)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
LargestPoolSize

Returns the largest number of threads that have ever simultaneously been in the pool.

(Inherited from ThreadPoolExecutor)
MaximumPoolSize

Returns the maximum allowed number of threads. -or- Sets the maximum allowed number of threads.

(Inherited from ThreadPoolExecutor)
PeerReference (Inherited from Object)
PoolSize

Returns the current number of threads in the pool.

(Inherited from ThreadPoolExecutor)
Queue

Returns the task queue used by this executor.

(Inherited from ThreadPoolExecutor)
RejectedExecutionHandler

Returns the current handler for unexecutable tasks. -or- Sets a new handler for unexecutable tasks.

(Inherited from ThreadPoolExecutor)
RemoveOnCancelPolicy

Gets the policy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation. -or- Sets the policy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation.

TaskCount

Returns the approximate total number of tasks that have ever been scheduled for execution.

(Inherited from ThreadPoolExecutor)
ThreadFactory

Returns the thread factory used to create new threads. -or- Sets the thread factory used to create new threads.

(Inherited from ThreadPoolExecutor)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

AfterExecute(IRunnable, Throwable)

Method invoked upon completion of execution of the given Runnable.

(Inherited from ThreadPoolExecutor)
AllowCoreThreadTimeOut(Boolean)

Sets the policy governing whether core threads may time out and terminate if no tasks arrive within the keep-alive time, being replaced if needed when new tasks arrive.

(Inherited from ThreadPoolExecutor)
AllowsCoreThreadTimeOut()

Returns true if this pool allows core threads to time out and terminate if no tasks arrive within the keepAlive time, being replaced if needed when new tasks arrive.

(Inherited from ThreadPoolExecutor)
AwaitTermination(Int64, TimeUnit) (Inherited from ThreadPoolExecutor)
AwaitTerminationAsync(Int64, TimeUnit) (Inherited from AbstractExecutorService)
BeforeExecute(Thread, IRunnable)

Method invoked prior to executing the given Runnable in the given thread.

(Inherited from ThreadPoolExecutor)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
DecorateTask(ICallable, IRunnableScheduledFuture)

Modifies or replaces the task used to execute a callable.

DecorateTask(IRunnable, IRunnableScheduledFuture)

Modifies or replaces the task used to execute a runnable.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Execute(IRunnable)

Executes the given task sometime in the future.

(Inherited from ThreadPoolExecutor)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetKeepAliveTime(TimeUnit)

Returns the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.

(Inherited from ThreadPoolExecutor)
InvokeAll(ICollection) (Inherited from AbstractExecutorService)
InvokeAll(ICollection, Int64, TimeUnit) (Inherited from AbstractExecutorService)
InvokeAny(ICollection) (Inherited from AbstractExecutorService)
InvokeAny(ICollection, Int64, TimeUnit) (Inherited from AbstractExecutorService)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
NewTaskFor(ICallable)

Returns a RunnableFuture for the given callable task.

(Inherited from AbstractExecutorService)
NewTaskFor(IRunnable, Object)

Returns a RunnableFuture for the given runnable and default value.

(Inherited from AbstractExecutorService)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
PrestartAllCoreThreads()

Starts all core threads, causing them to idly wait for work.

(Inherited from ThreadPoolExecutor)
PrestartCoreThread()

Starts a core thread, causing it to idly wait for work.

(Inherited from ThreadPoolExecutor)
Purge()

Tries to remove from the work queue all Future tasks that have been cancelled.

(Inherited from ThreadPoolExecutor)
Remove(IRunnable)

Removes this task from the executor's internal queue if it is present, thus causing it not to be run if it has not already started.

(Inherited from ThreadPoolExecutor)
Schedule(ICallable, Int64, TimeUnit)
Schedule(IRunnable, Int64, TimeUnit)

Creates and executes a one-shot action that becomes enabled after the given delay.

ScheduleAtFixedRate(IRunnable, Int64, Int64, TimeUnit)

Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is, executions will commence after initialDelay, then initialDelay + period, then initialDelay + 2 * period, and so on.

ScheduleWithFixedDelay(IRunnable, Int64, Int64, TimeUnit)

Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetKeepAliveTime(Int64, TimeUnit)

Sets the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.

(Inherited from ThreadPoolExecutor)
Shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

(Inherited from ThreadPoolExecutor)
ShutdownNow()

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

(Inherited from ThreadPoolExecutor)
Submit(ICallable) (Inherited from AbstractExecutorService)
Submit(IRunnable)

Submits a Runnable task for execution and returns a Future representing that task.

(Inherited from AbstractExecutorService)
Submit(IRunnable, Object) (Inherited from AbstractExecutorService)
Terminated()

Method invoked when the Executor has terminated.

(Inherited from ThreadPoolExecutor)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
AwaitTerminationAsync(IExecutorService, Int64, TimeUnit)
InvokeAnyAsync(IExecutorService, ICollection)
InvokeAnyAsync(IExecutorService, ICollection, Int64, TimeUnit)

Applies to