Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.util.executor;
import alluxio.annotation.SuppressFBWarnings;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A controllable scheduler supports jump to a future time and run all
* commands within that time period.
* This class is designed for tests using {@link ScheduledExecutorService}.
*/
public class ControllableScheduler implements ScheduledExecutorService {
private final ControllableQueue> mQueue = new ControllableQueue<>();
/**
* Jumps to a future time by a given duration. All the commands/tasks scheduled
* for execution during this period will be executed. When this function returns,
* the executor will be idle.
*
* @param duration the duration
* @param timeUnit the time unit of the duration
*/
public void jumpAndExecute(long duration, TimeUnit timeUnit) {
long durationInMillis = TimeUnit.MILLISECONDS.convert(duration, timeUnit);
mQueue.tick(durationInMillis);
while (!schedulerIsIdle()) {
runNextPendingCommand();
}
}
/**
* Reports whether scheduler has no commands/tasks pending immediate execution.
*
* @return true if there are no commands pending immediate execution, false otherwise
*/
public boolean schedulerIsIdle() {
return mQueue.isEmpty() || mQueue.getHeadDelay() > 0;
}
/**
* Runs the next command scheduled to be executed immediately.
*/
public void runNextPendingCommand() {
long peakDelay = mQueue.getHeadDelay();
ScheduledTask> scheduledTask = mQueue.pop();
scheduledTask.run();
if (!scheduledTask.isCancelled() && scheduledTask.isRepeat()) {
mQueue.add(scheduledTask.mRepeatDelay + peakDelay, scheduledTask);
}
}
@Override
public void execute(Runnable command) {
schedule(command, 0, TimeUnit.SECONDS);
}
@Override
public ScheduledFuture> schedule(Runnable command, long delay, TimeUnit unit) {
ScheduledTask task = new ScheduledTask(command);
mQueue.add(TimeUnit.MILLISECONDS.convert(delay, unit), task);
return task;
}
@Override
public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
ScheduledTask task = new ScheduledTask(callable);
mQueue.add(TimeUnit.MILLISECONDS.convert(delay, unit), task);
return task;
}
@Override
public ScheduledFuture> scheduleAtFixedRate(Runnable command,
long initialDelay, long period, TimeUnit unit) {
return scheduleWithFixedDelay(command, initialDelay, period, unit);
}
@Override
public ScheduledFuture> scheduleWithFixedDelay(Runnable command,
long initialDelay, long delay, TimeUnit unit) {
ScheduledTask