Android Guides | Samples

Java.Util.Concurrent.ScheduledThreadPoolExecutor Class

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

See Also: ScheduledThreadPoolExecutor

Syntax

[Android.Runtime.Register("java/util/concurrent/ScheduledThreadPoolExecutor", DoNotGenerateAcw=true)]
public class ScheduledThreadPoolExecutor : ThreadPoolExecutor, IScheduledExecutorService, IDisposable

Remarks

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to 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 ScheduledThreadPoolExecutor.ScheduleAtFixedRate(IRunnable,Int64,Int64,Int64) or ScheduledThreadPoolExecutor.ScheduleWithFixedDelay(IRunnable,Int64,Int64,Int64) do not overlap. While different executions may be performed by different threads, the effects of prior executions 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.

Extension notes: This class overrides the ThreadPoolExecutor.Execute(IRunnable) and AbstractExecutorService.Submit(IRunnable) methods to generate internal IScheduledFuture 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:

java Example

public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {

   static class CustomTask implements RunnableScheduledFuture { ... 

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

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

[Android Documentation]

Requirements

Namespace: Java.Util.Concurrent
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Java.Util.Concurrent.ScheduledThreadPoolExecutor are listed below.

See Also: ThreadPoolExecutor

Public Constructors

Creates a new ScheduledThreadPoolExecutor with the given core pool size.
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.
Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

Protected Constructors

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

Public Properties

ContinueExistingPeriodicTasksAfterShutdownPolicyBoolean. Gets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown.
ExecuteExistingDelayedTasksAfterShutdownPolicyBoolean. Gets the policy on whether to execute existing delayed tasks even when this executor has been shutdown.
RemoveOnCancelPolicyBoolean. Gets the policy on whether cancelled tasks should be immediately removed from the work queue at time of cancellation.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

Schedule(IRunnable, Int64, TimeUnit) : IScheduledFuture
Creates and executes a one-shot action that becomes enabled after the given delay.
Schedule(ICallable, Int64, TimeUnit) : IScheduledFuture
ScheduleAtFixedRate(IRunnable, Int64, Int64, TimeUnit) : IScheduledFuture
Creates and executes 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) : IScheduledFuture
Creates and executes 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.

Protected Methods