| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
IdleSerialScheduler is a small Java utility for running short processing ticks strictly one at a time on top of a shared executor pool.
Its main property is that while no work exists, it consumes essentially no execution resources:
It becomes active only when external code explicitly signals new work via kick().
It is designed for background loops that:
Typical use cases:
A common naive approach looks like this:
while (true) {
if (hasWork()) {
runOnce();
}
Thread.sleep(1000);
}This has obvious drawbacks:
In contrast, IdleSerialScheduler does not keep any background activity alive on its own while idle.
If no work exists, nothing is spinning, sleeping, polling, or waking up periodically.
IdleSerialScheduler follows a different model:
Execution model:
new work -> kick() -> runOnce()
-> if work remains -> delay -> next runOnce()
-> if no work remains -> idle
Important: when new work arrives, external code must call kick().
ExecutorService backendPool = Executors.newFixedThreadPool(4);
AtomicInteger remaining = new AtomicInteger(10);
IdleSerialScheduler scheduler = new IdleSerialScheduler(
backendPool,
1,
TimeUnit.SECONDS,
() -> {
// process one small batch / one tick
System.out.println("tick");
remaining.decrementAndGet();
},
() -> remaining.get() > 0,
error -> error.printStackTrace()
);
// signal that work is available
scheduler.kick();Main constructor:
public IdleSerialScheduler(
Executor backendPool,
long delay,
TimeUnit unit,
Runnable runOnce,
BooleanSupplier hasWork,
Consumer<Throwable> errorHandler
)Parameters:
A low-level utility that guarantees strict sequential execution of submitted tasks on top of a backend executor.
There are two different failure categories.
If user task code throws:
If the backend executor rejects task submission:
This utility provides best-effort coalescing.
That means:
This is a deliberate tradeoff for keeping the implementation small, predictable, and robust.
Also note:
The project includes JUnit 5 tests for:
MIT
| Back | FazBrowse Home | New Git URL |