24 lines
656 B
Java
24 lines
656 B
Java
package com.example.leakdemo;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.function.Consumer;
|
|
|
|
/** Classic Vaadin broadcaster. Fine by itself, leaks when listeners are never removed. */
|
|
public final class Broadcaster {
|
|
|
|
private static final List<Consumer<String>> LISTENERS = new CopyOnWriteArrayList<>();
|
|
|
|
private Broadcaster() {
|
|
}
|
|
|
|
public static Runnable register(Consumer<String> listener) {
|
|
LISTENERS.add(listener);
|
|
return () -> LISTENERS.remove(listener);
|
|
}
|
|
|
|
public static void broadcast(String message) {
|
|
LISTENERS.forEach(l -> l.accept(message));
|
|
}
|
|
}
|