Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
The runner interface defines an object responsible for executing routine invocations inside specifically managed threads.

The implementation can both be synchronous or asynchronous, it can allocate specific threads or share a pool of them between different instances.
The only requirement is that the specified execution is called each time a run method is invoked.
Note that, a proper asynchronous runner implementation will never synchronously run an execution, no matter the delay, unless it employs a single thread. While, a proper synchronous runner, will always run executions in the very same caller thread.
Note also that the runner methods can be called from different threads, so, it is up to the implementing class to ensure synchronization when required.

The implementing class can optionally support the cancellation of executions not yet run ( waiting, for example, in a consuming queue).

Created by davide-maestroni on 9/7/14.

public interface Runner {

Cancels the specified execution if not already run.
Note that the method will have no effect in case the runner does not maintain a queue or the specified execution has been already processed at the moment of the call.
Note also that, in case the same execution has been added more than one time to the runner queue, when the method returns, the queue will not contain the execution instance anymore, with the consequence that the method will never be called.

The implementation of this method is optional, still, it may greatly increase the performance by avoiding to start invocations which are already canceled. The specific implementation can safely ignore all that executions whose method Execution.mayBeCanceled() returns false.

execution the execution.
    void cancel(@Nonnull Execution execution);

Checks if the calling thread belongs to the ones employed by the object to run executions.

The implementation of this method is not strictly mandatory, even if, the classes always returning false effectively prevent the correct detection of possible deadlocks.
A synchronous runner implementation will always return false.

whether the calling thread is managed by the runner.
    boolean isExecutionThread();

Runs the specified execution (that is, it calls the method inside the runner thread).

execution the execution.
delay the execution delay.
timeUnit the delay time unit.
    void run(@Nonnull Execution executionlong delay, @Nonnull TimeUnit timeUnit);
New to GrepCode? Check out our FAQ X