use of org.spongepowered.api.scheduler.ScheduledTask in project SpongeCommon by SpongePowered.
the class AsyncScheduler method close.
public void close() {
this.running = false;
// Cancel all tasks
final Set<ScheduledTask> tasks = this.tasks();
tasks.forEach(ScheduledTask::cancel);
// Shut down the executor
this.executor.shutdown();
try {
if (!this.executor.awaitTermination(5, TimeUnit.SECONDS)) {
new PrettyPrinter().add("Sponge async scheduler failed to shut down in 5 seconds! Tasks that may have been active:").addWithIndices(tasks).add().add("We will now attempt immediate shutdown.").log(SpongeCommon.logger(), Level.WARN);
this.executor.shutdownNow();
}
} catch (final InterruptedException e) {
SpongeCommon.logger().error("The async scheduler was interrupted while awaiting shutdown!");
}
}
use of org.spongepowered.api.scheduler.ScheduledTask in project SpongeCommon by SpongePowered.
the class SpongeScheduler method findTasks.
@Override
public Set<ScheduledTask> findTasks(final String pattern) {
final Pattern searchPattern = Pattern.compile(Objects.requireNonNull(pattern, "pattern"));
final Set<ScheduledTask> matchingTasks = this.tasks();
final Iterator<ScheduledTask> it = matchingTasks.iterator();
while (it.hasNext()) {
final Matcher matcher = searchPattern.matcher(it.next().name());
if (!matcher.matches()) {
it.remove();
}
}
return matchingTasks;
}
use of org.spongepowered.api.scheduler.ScheduledTask in project SpongeCommon by SpongePowered.
the class AsyncScheduler method recalibrateMinimumTimeout.
private void recalibrateMinimumTimeout() {
this.lock.lock();
try {
final Set<ScheduledTask> tasks = this.tasks();
this.minimumTimeout = Long.MAX_VALUE;
final long now = System.nanoTime();
for (final ScheduledTask tmpTask : tasks) {
final SpongeScheduledTask task = (SpongeScheduledTask) tmpTask;
if (task.state() == SpongeScheduledTask.ScheduledTaskState.EXECUTING) {
// bail out for this task. We'll signal when we complete the task.
continue;
}
// tasks cause the scheduler to process pending tasks.
if (task.task.delay == 0 && task.task.interval == 0) {
this.minimumTimeout = 0;
}
// The time since the task last executed or was added to the map
final long timeSinceLast = now - task.timestamp();
if (task.task.delay > 0 && task.state() == SpongeScheduledTask.ScheduledTaskState.WAITING) {
// There is an delay and the task hasn't run yet
this.minimumTimeout = Math.min(task.task.delay - timeSinceLast, this.minimumTimeout);
}
if (task.task.interval > 0 && task.state().isActive) {
// The task repeats and has run after the initial delay
this.minimumTimeout = Math.min(task.task.interval - timeSinceLast, this.minimumTimeout);
}
if (this.minimumTimeout <= 0) {
break;
}
}
if (!tasks.isEmpty()) {
final long latency = System.nanoTime() - this.lastProcessingTimestamp;
this.minimumTimeout -= (latency <= 0) ? 0 : latency;
this.minimumTimeout = (this.minimumTimeout < 0) ? 0 : this.minimumTimeout;
}
} finally {
this.lock.unlock();
}
}
use of org.spongepowered.api.scheduler.ScheduledTask in project SpongeCommon by SpongePowered.
the class SpongeTaskExecutorService method scheduleAtFixedRate.
@Override
public ScheduledTaskFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TemporalUnit unit) {
final RepeatableFutureTask<?> runnable = new RepeatableFutureTask<>(command);
final Task task = this.createTask(runnable).delay(initialDelay, unit).interval(period, unit).build();
final SpongeScheduledTask scheduledTask = this.submitTask(task);
// A repeatable task needs to be able to cancel itself
runnable.setTask(scheduledTask);
return new SpongeScheduledFuture<>(runnable, scheduledTask, this.scheduler);
}
use of org.spongepowered.api.scheduler.ScheduledTask in project SpongeCommon by SpongePowered.
the class SpongeTaskExecutorService method scheduleAtFixedRate.
@Override
public ScheduledTaskFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) {
final RepeatableFutureTask<?> runnable = new RepeatableFutureTask<>(command);
final Task task = this.createTask(runnable).delay(initialDelay, unit).interval(period, unit).build();
final SpongeScheduledTask scheduledTask = this.submitTask(task);
// A repeatable task needs to be able to cancel itself
runnable.setTask(scheduledTask);
return new SpongeScheduledFuture<>(runnable, scheduledTask, this.scheduler);
}
Aggregations