Debouncer防抖代码
Debouncer类
import java.util.concurrent.*;
public class Debouncer {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final ConcurrentHashMap
测试
@Test
public void debouncer() throws InterruptedException {
Debouncer debouncer = new Debouncer();
for (int i = 0; i < 10; i++) {
System.out.println("start---------" + System.currentTimeMillis());
debouncer.debounce("test_key",
() -> {System.out.println("run ----------" + System.currentTimeMillis());},
1, TimeUnit.SECONDS);
}
Thread.sleep(8000);
}