From ca40e545fa1e02c1d2b8994a43333f2e72345e9c Mon Sep 17 00:00:00 2001 From: Marcel Verpaalen Date: Fri, 27 Aug 2021 12:20:35 +0200 Subject: [PATCH 1/2] [miio] Avoid shutting down shared executor service * Closing #11152 * Better identify communication threads Signed-off-by: Marcel Verpaalen --- .../internal/handler/MiIoAbstractHandler.java | 3 ++- .../transport/MiIoAsyncCommunication.java | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java index 93acefd1b46e3..6d5a23d480604 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java @@ -77,7 +77,8 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi protected static final Gson GSON = new GsonBuilder().create(); protected static final String TIMESTAMP = "timestamp"; - protected ScheduledExecutorService miIoScheduler = scheduler; + protected ScheduledExecutorService miIoScheduler = new ScheduledThreadPoolExecutor(1, + new NamedThreadFactory("miio")); protected @Nullable ScheduledFuture pollingJob; protected MiIoDevices miDevice = MiIoDevices.UNKNOWN; protected boolean isIdentified; diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java index 63a75a4d55dd9..d0a8e9d746a4b 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java @@ -249,7 +249,7 @@ MiIoSendCommand sendMiIoSendCommand(MiIoSendCommand miIoSendCommand) { public synchronized void startReceiver() { MessageSenderThread senderThread = this.senderThread; if (senderThread == null || !senderThread.isAlive()) { - senderThread = new MessageSenderThread(); + senderThread = new MessageSenderThread(deviceId); senderThread.start(); this.senderThread = senderThread; } @@ -261,14 +261,17 @@ public synchronized void startReceiver() { * */ private class MessageSenderThread extends Thread { - public MessageSenderThread() { - super("Mi IO MessageSenderThread"); + private final String deviceId; + + public MessageSenderThread(String deviceId) { + super("Mi IO MessageSenderThread - " + deviceId); setDaemon(true); + this.deviceId = deviceId; } @Override public void run() { - logger.debug("Starting Mi IO MessageSenderThread"); + logger.debug("Starting Mi IO MessageSenderThread {}", deviceId); while (!interrupted()) { try { if (concurrentLinkedQueue.isEmpty()) { @@ -291,11 +294,11 @@ public void run() { // That's our signal to stop break; } catch (Exception e) { - logger.warn("Error while polling/sending message", e); + logger.warn("Error while polling/sending message for {}", deviceId, e); } } closeSocket(); - logger.debug("Finished Mi IO MessageSenderThread"); + logger.debug("Finished Mi IO MessageSenderThread {}", deviceId); } } From 9d87f88bbe55b69bd136d97c4f55682bca27cc6c Mon Sep 17 00:00:00 2001 From: Marcel Verpaalen Date: Sun, 29 Aug 2021 19:40:19 +0200 Subject: [PATCH 2/2] [miio] rename threads Signed-off-by: Marcel Verpaalen --- .../binding/miio/internal/handler/MiIoAbstractHandler.java | 7 ++++--- .../miio/internal/transport/MiIoAsyncCommunication.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java index 6d5a23d480604..b97d68cd10d86 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java @@ -77,8 +77,9 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi protected static final Gson GSON = new GsonBuilder().create(); protected static final String TIMESTAMP = "timestamp"; - protected ScheduledExecutorService miIoScheduler = new ScheduledThreadPoolExecutor(1, - new NamedThreadFactory("miio")); + protected ScheduledExecutorService miIoScheduler = new ScheduledThreadPoolExecutor(3, + new NamedThreadFactory("binding-" + getThing().getUID().getAsString(), true)); + protected @Nullable ScheduledFuture pollingJob; protected MiIoDevices miDevice = MiIoDevices.UNKNOWN; protected boolean isIdentified; @@ -135,7 +136,7 @@ public void initialize() { getThing().getThingTypeUID()); ScheduledThreadPoolExecutor miIoScheduler = new ScheduledThreadPoolExecutor(3, - new NamedThreadFactory(getThing().getUID().getAsString(), true)); + new NamedThreadFactory("binding-" + getThing().getUID().getAsString(), true)); miIoScheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); miIoScheduler.setRemoveOnCancelPolicy(true); this.miIoScheduler = miIoScheduler; diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java index d0a8e9d746a4b..95be2fba35aa8 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/transport/MiIoAsyncCommunication.java @@ -264,7 +264,7 @@ private class MessageSenderThread extends Thread { private final String deviceId; public MessageSenderThread(String deviceId) { - super("Mi IO MessageSenderThread - " + deviceId); + super("OH-binding-miio-MessageSenderThread-" + deviceId); setDaemon(true); this.deviceId = deviceId; }