Java版的防抖(debounce)和节流(throttle)
概念
防抖(debounce)
当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定时间到来之前,又触发了事件,就重新开始延时
节流(throttle)
当持续触发事件时,保证在一定时间内只调用一次事件处理函数,意思就是说,假设一个用户一直触发这个函数,且每次触发小于既定值,函数节流会每隔这个时间调用一次
区别
防抖是将多次执行变为最后一次执行,节流是将多次执行变为每隔一段时间执行
Java实现
防抖(debounce)
public class DebounceTask {
private Timer timer;
private Long delay;
private Runnable runnable;
public DebounceTask(Runnable runnable, Long delay) {
this.runnable = runnable;
this.delay = delay;
}
public static DebounceTask build(Runnable runnable, Long delay){
return new DebounceTask(runnable, delay);
}
public void run(){
if(timer!=null){
timer.cancel();
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
timer=null;
runnable.run();
}
}, delay);
}
}
节流(throttle)
public class ThrottleTask {
private Timer timer;
private Long delay;
private Runnable runnable;
private boolean needWait=false;
public ThrottleTask(Runnable runnable, Long delay) {
this.runnable = runnable;
this.delay = delay;
this.timer = new Timer();
}
public static ThrottleTask build(Runnable runnable, Long delay){
return new ThrottleTask(runnable, delay);
}
public void run(){
if(!needWait){
needWait=true;
timer.schedule(new TimerTask() {
@Override
public void run() {
needWait=false;
runnable.run();
}
}, delay);
}
}
}
测试
防抖(debounce)
DebounceTask task = DebounceTask.build(new Runnable() {
@Override
public void run() {
System.out.println("do task: "+System.currentTimeMillis());
}
},1000L);
long delay = 100;
while (true){
System.out.println("call task: "+System.currentTimeMillis());
task.run();
delay+=100;
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
节流(throttle)
ThrottleTask task = ThrottleTask.build(new Runnable() {
@Override
public void run() {
System.out.println("do task: "+System.currentTimeMillis());
}
},1000L);
while (true){
System.out.println("call task: "+System.currentTimeMillis());
task.run();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}