org.eclipse.jetty.util.thread.Scheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetty-util Show documentation
Show all versions of jetty-util Show documentation
Utility classes for Jetty
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.component.LifeCycle;
/**
* Schedules tasks to be executed after a delay.
*/
public interface Scheduler extends LifeCycle
{
/**
* A delayed task that can be cancelled.
*/
interface Task
{
/**
* Attempts to cancel the execution of this task.
* If the task is already cancelled, or already executed,
* this method has no effect and returns {@code false}.
* Otherwise, the execution of this task is cancelled
* and this method returns {@code true}.
*
* @return whether the task was cancelled
*/
boolean cancel();
}
/**
* Schedules a task to be executed after the given delay.
*
* @param task the task to execute
* @param delay the delay value
* @param units the unit of time of the delay
* @return a delayed task
*/
Task schedule(Runnable task, long delay, TimeUnit units);
/**
* Schedules a task to be executed after the given delay.
*
* @param task the task to execute
* @param delay the delay duration
* @return a delayed task
*/
default Task schedule(Runnable task, Duration delay)
{
return schedule(task, delay.toNanos(), TimeUnit.NANOSECONDS);
}
}