From 34aa5229380c24fbf0150e405a9bdb335116a478 Mon Sep 17 00:00:00 2001 From: Gareth Randall Date: Sun, 17 Dec 2023 14:57:44 +0000 Subject: [PATCH] Use single threaded event delivery to avoid incorrect ordering. The JVM does not make guarantees about the order that threads run in and under heavy load this leads to events being delivered to listeners in a different order to that in what they arrived. --- src/main/java/xyz/cp74/evdev/InputDevice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/xyz/cp74/evdev/InputDevice.java b/src/main/java/xyz/cp74/evdev/InputDevice.java index 3df6e72..877e54b 100644 --- a/src/main/java/xyz/cp74/evdev/InputDevice.java +++ b/src/main/java/xyz/cp74/evdev/InputDevice.java @@ -52,6 +52,7 @@ public class InputDevice implements InputDevicePath { private int maxConnectionAttemps = 10; // thread pool + private final ExecutorService globalExecutor = Executors.newSingleThreadExecutor(); ExecutorService threadPool = Executors.newCachedThreadPool(); // listeners @@ -259,7 +260,7 @@ attempts < getMaxConnectionAttemps()) { e.value = buffer.getInt(); // send event to listeners if (globalListener!=null) - threadPool.execute(()->globalListener.onEvent(e)); + globalExecutor.execute(()->globalListener.onEvent(e)); if (eventListeners.containsKey(e.getTypeCode())) threadPool.execute(()->eventListeners.get(e.getTypeCode()).onEvent(e)); }