java
47 lines · 9 steps
How a debouncer collapses bursts in Java
A generic debouncer delays an action and cancels pending runs so only the last value in a burst fires.
Explained by
highlit
1public final class Debouncer<T> {
2
3 private final ScheduledExecutorService scheduler =
4 Executors.newSingleThreadScheduledExecutor(runnable -> {
5 Thread thread = new Thread(runnable, "debouncer");
6 thread.setDaemon(true);
7 return thread;
8 });
9
10 private final long delay;
11 private final TimeUnit unit;
12 private final Consumer<T> action;
13 private final Object lock = new Object();
14
15 private ScheduledFuture<?> pending;
16 private T latest;
17
18 public Debouncer(long delay, TimeUnit unit, Consumer<T> action) {
19 this.delay = delay;
20 this.unit = unit;
21 this.action = action;
22 }
23
24 public void submit(T value) {
25 synchronized (lock) {
26 latest = value;
27 if (pending != null) {
28 pending.cancel(false);
29 }
30 pending = scheduler.schedule(this::fire, delay, unit);
31 }
32 }
33
34 private void fire() {
35 T value;
36 synchronized (lock) {
37 value = latest;
38 pending = null;
39 latest = null;
40 }
41 action.accept(value);
42 }
43
44 public void shutdown() {
45 scheduler.shutdownNow();
46 }
47}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Debouncing waits out a quiet period and only acts on the final input of a burst.
- 2Cancelling the pending task on each new input is what collapses rapid calls into one.
- 3A single daemon scheduler plus a lock keeps timing correct without blocking JVM shutdown.
Related explainers
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
typescript
type CsvColumn<T> = { header: string; value: (row: T) => string | number | boolean | null | undefined; };
Building a type-safe CSV writer in TypeScript
generics
serialization
escaping
Intermediate
7 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-debouncer-collapses-bursts-in-java-explained-java-e8cc/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.