VtScheduler is an in-memory scheduler built using Java Virtual Threads (JDK 21+). We designed it to be a simple, dependency-free solution for managing periodic or background tasks without the need for complex frameworks, databases, or external services.
- Virtual Thread–Powered: This is our core focus. Leveraging the "blocking is cheap" principle of Virtual Threads, the scheduler can handle a high volume of concurrent tasks with minimal resource usage, especially in I/O-bound scenarios.
- Simple, Pure Java API: Provides clean methods like
schedule(),scheduleAtFixedRate(), andscheduleWithDelay()for effortless integration. - CronTrigger Support: Easily set flexible schedules using standard six-field Cron expressions (e.g.
0 15 * * * *). - Zero Dependencies: It's pure Java, making it easy to embed into any JVM application.
- Graceful Shutdown: Safely waits for ongoing tasks to complete before stopping, minimizing the risk of half-finished operations.
import java.time.Duration;
var scheduler = VtScheduler.create()
.onError((jobId, error) -> {
// Handle exceptions that occur during job execution
System.err.printf("[Error] Job %s failed: %s%n", jobId, error.getMessage());
});
// Schedules a Cron job to run every day at 3:00 PM
scheduler.schedule(() ->
System.out.println("Cron job executed."),
new CronTrigger("0 15 * * * *"));
// Schedules a task to run repeatedly every 5 seconds
scheduler.scheduleAtFixedRate(() ->
System.out.println("Heartbeat pulse"),
Duration.ofSeconds(5));When shutting down, VtScheduler:
- Stops accepting new jobs
- Waits up to 10 seconds for running jobs to complete
- Cancels any jobs that do not finish in time
- Reports any timeouts through the
onError()handler
This ensures ongoing tasks can finish cleanly before the scheduler stops.
- Internally uses
DelayQueuefor precise timing - All jobs run on virtual threads, making blocking operations inexpensive
- Runs entirely in-memory — no database or persistence layer