|
15 | 15 | */ |
16 | 16 | package nextflow.lsp.util; |
17 | 17 |
|
18 | | -import java.util.concurrent.ConcurrentHashMap; |
19 | 18 | import java.util.concurrent.Executors; |
20 | 19 | import java.util.concurrent.ScheduledExecutorService; |
| 20 | +import java.util.concurrent.ScheduledFuture; |
21 | 21 | import java.util.concurrent.TimeUnit; |
22 | | -import java.util.function.Consumer; |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * Executor service that debounces incoming tasks, so |
26 | 26 | * that a task is executed only after not being triggered |
27 | | - * for a given time period. |
28 | | - * |
29 | | - * see: https://stackoverflow.com/questions/4742210/implementing-debounce-in-java/20978973 |
| 27 | + * for a given delay. |
30 | 28 | * |
31 | 29 | * @author Ben Sherman <bentshermann@gmail.com> |
32 | 30 | */ |
33 | | -public class DebouncingExecutor<T> { |
34 | | - private int delayMillis; |
35 | | - private Consumer<T> action; |
36 | | - private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
37 | | - private ConcurrentHashMap<T, DelayedTask> delayedTasks = new ConcurrentHashMap<>(); |
| 31 | +public class DebouncingExecutor { |
| 32 | + private final long delayMillis; |
| 33 | + private final Runnable action; |
| 34 | + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 35 | + private final AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>(); |
38 | 36 |
|
39 | | - public DebouncingExecutor(int delayMillis, Consumer<T> action) { |
| 37 | + public DebouncingExecutor(long delayMillis, Runnable action) { |
40 | 38 | this.delayMillis = delayMillis; |
41 | 39 | this.action = action; |
42 | 40 | } |
43 | 41 |
|
44 | | - public void submit(T key) { |
45 | | - var newTask = new DelayedTask(key); |
| 42 | + /** |
| 43 | + * Schedule the action after the configured delay, cancelling |
| 44 | + * the currently scheduled task if present. |
| 45 | + */ |
| 46 | + public synchronized void executeLater() { |
| 47 | + cancelExisting(); |
46 | 48 |
|
47 | | - // try until new task was added, or existing task was extended |
48 | | - DelayedTask oldTask; |
49 | | - do { |
50 | | - oldTask = delayedTasks.putIfAbsent(key, newTask); |
51 | | - if( oldTask == null ) |
52 | | - executor.schedule(newTask, delayMillis, TimeUnit.MILLISECONDS); |
53 | | - } while( oldTask != null && !oldTask.extend() ); |
54 | | - } |
| 49 | + var future = scheduler.schedule(() -> { |
| 50 | + action.run(); |
| 51 | + futureRef.set(null); |
| 52 | + }, delayMillis, TimeUnit.MILLISECONDS); |
55 | 53 |
|
56 | | - public void executeNow(T key) { |
57 | | - var task = delayedTasks.get(key); |
58 | | - if( task != null ) |
59 | | - task.cancel(); |
60 | | - action.accept(key); |
| 54 | + futureRef.set(future); |
61 | 55 | } |
62 | 56 |
|
63 | | - public void shutdownNow() { |
64 | | - executor.shutdownNow(); |
| 57 | + /** |
| 58 | + * Execute the action immediately, cancelling the currently |
| 59 | + * scheduled task if present. |
| 60 | + */ |
| 61 | + public synchronized void executeNow() { |
| 62 | + cancelExisting(); |
| 63 | + action.run(); |
65 | 64 | } |
66 | 65 |
|
67 | | - private class DelayedTask implements Runnable { |
68 | | - private T key; |
69 | | - private long dueTime; |
70 | | - private Object lock = new Object(); |
71 | | - |
72 | | - public DelayedTask(T key) { |
73 | | - this.key = key; |
74 | | - extend(); |
75 | | - } |
76 | | - |
77 | | - public boolean extend() { |
78 | | - synchronized (lock) { |
79 | | - if( dueTime < 0 ) |
80 | | - return false; |
81 | | - dueTime = System.currentTimeMillis() + delayMillis; |
82 | | - return true; |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - public void cancel() { |
87 | | - synchronized (lock) { |
88 | | - dueTime = -1; |
89 | | - delayedTasks.remove(key); |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - public void run() { |
94 | | - synchronized (lock) { |
95 | | - var remaining = dueTime - System.currentTimeMillis(); |
96 | | - if( remaining > 0 ) { |
97 | | - // re-schedule task |
98 | | - executor.schedule(this, remaining, TimeUnit.MILLISECONDS); |
99 | | - } |
100 | | - else if( dueTime != -1 ) { |
101 | | - // mark task as terminated and invoke callback |
102 | | - dueTime = -1; |
103 | | - try { |
104 | | - action.accept(key); |
105 | | - } |
106 | | - catch( Exception e ) { |
107 | | - System.err.println("exception while invoking debounce callback: " + e.toString()); |
108 | | - e.printStackTrace(System.err); |
109 | | - } |
110 | | - finally { |
111 | | - delayedTasks.remove(key); |
112 | | - } |
113 | | - } |
114 | | - } |
115 | | - } |
| 66 | + private void cancelExisting() { |
| 67 | + var existing = futureRef.getAndSet(null); |
| 68 | + if( existing != null && !existing.isDone() ) |
| 69 | + existing.cancel(false); |
116 | 70 | } |
117 | 71 |
|
| 72 | + /** |
| 73 | + * Call this method to shut down the executor when no longer needed. |
| 74 | + */ |
| 75 | + public void shutdown() { |
| 76 | + scheduler.shutdownNow(); |
| 77 | + } |
118 | 78 | } |
0 commit comments