fix viewable thread-safety

This commit is contained in:
Pyrbu 2025-04-02 23:35:14 +02:00
parent ffe3d3dc35
commit affa0f4c84

View file

@ -11,14 +11,16 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public abstract class Viewable { public abstract class Viewable {
private final static List<WeakReference<Viewable>> all = new ArrayList<>(); private final static List<WeakReference<Viewable>> all = Collections.synchronizedList(new ArrayList<>());
public static List<Viewable> all() { public static List<Viewable> all() {
synchronized (all) {
all.removeIf(reference -> reference.get() == null); all.removeIf(reference -> reference.get() == null);
return all.stream() return all.stream()
.map(Reference::get) .map(Reference::get)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
}
private boolean queueRunning = false; private boolean queueRunning = false;
private final Queue<Runnable> visibilityTaskQueue = new ConcurrentLinkedQueue<>(); private final Queue<Runnable> visibilityTaskQueue = new ConcurrentLinkedQueue<>();