]> git.basschouten.com Git - openhab-addons.git/commitdiff
[rotel] Fix reader thread handling (#14272)
authorlolodomo <lg.hc@free.fr>
Fri, 27 Jan 2023 12:23:04 +0000 (13:23 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Jan 2023 12:23:04 +0000 (13:23 +0100)
Previous code was not working in Java 17.
A thread should not be started after being interrupted.

Fix #14264

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.rotel/src/main/java/org/openhab/binding/rotel/internal/communication/RotelConnector.java
bundles/org.openhab.binding.rotel/src/main/java/org/openhab/binding/rotel/internal/communication/RotelIpConnector.java
bundles/org.openhab.binding.rotel/src/main/java/org/openhab/binding/rotel/internal/communication/RotelSerialConnector.java
bundles/org.openhab.binding.rotel/src/main/java/org/openhab/binding/rotel/internal/communication/RotelSimuConnector.java

index 778d11367de1a2e6201e003f21c8f6f030eed15a..4dc0cbc317221d2178d89c71e03f00f7a9a80b84 100644 (file)
@@ -34,8 +34,11 @@ public abstract class RotelConnector {
 
     private final Logger logger = LoggerFactory.getLogger(RotelConnector.class);
 
+    private final RotelAbstractProtocolHandler protocolHandler;
     private final boolean simu;
-    protected final Thread readerThread;
+    private final String readerThreadName;
+
+    private @Nullable Thread readerThread;
 
     /** The output stream */
     protected @Nullable OutputStream dataOut;
@@ -54,8 +57,9 @@ public abstract class RotelConnector {
      * @param readerThreadName the name of thread to be created
      */
     public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
+        this.protocolHandler = protocolHandler;
         this.simu = simu;
-        this.readerThread = new RotelReaderThread(this, protocolHandler, readerThreadName);
+        this.readerThreadName = readerThreadName;
     }
 
     /**
@@ -88,15 +92,32 @@ public abstract class RotelConnector {
      */
     public abstract void close();
 
+    protected void startReaderThread() {
+        Thread thread = readerThread;
+        if (thread == null || thread.isInterrupted()) {
+            thread = new RotelReaderThread(this, protocolHandler, readerThreadName);
+            readerThread = thread;
+            thread.start();
+        }
+    }
+
+    protected void stopReaderThread() {
+        Thread thread = readerThread;
+        if (thread != null) {
+            thread.interrupt();
+            try {
+                thread.join();
+            } catch (InterruptedException e) {
+            }
+            readerThread = null;
+        }
+    }
+
     /**
      * Stop the thread that handles the feedback messages and close the opened input and output streams
      */
     protected void cleanup() {
-        readerThread.interrupt();
-        try {
-            readerThread.join();
-        } catch (InterruptedException e) {
-        }
+        stopReaderThread();
         OutputStream dataOut = this.dataOut;
         if (dataOut != null) {
             try {
index 30b5e58e5f3fa80b1fc191d4f485343a4e3b3b77..ea971e754c707964d50bb3609dd7402df9a47c0a 100644 (file)
@@ -68,7 +68,7 @@ public class RotelIpConnector extends RotelConnector {
             dataOut = new DataOutputStream(clientSocket.getOutputStream());
             dataIn = new DataInputStream(clientSocket.getInputStream());
 
-            readerThread.start();
+            startReaderThread();
 
             this.clientSocket = clientSocket;
 
index 47571b0dee5b41ab664ebe34e44a8609122674fb..1c91803eae529755758a4763f18b9d55acf3ae5b 100644 (file)
@@ -94,7 +94,7 @@ public class RotelSerialConnector extends RotelConnector {
                 }
             }
 
-            readerThread.start();
+            startReaderThread();
 
             this.serialPort = commPort;
             this.dataIn = dataIn;
index 836cacb081b37c8d7c6bbdd92f4303e414edb623..35c6fabcb44929f9590382026c1b7bd092000251 100644 (file)
@@ -132,7 +132,7 @@ public class RotelSimuConnector extends RotelConnector {
     @Override
     public synchronized void open() throws RotelException {
         logger.debug("Opening simulated connection");
-        readerThread.start();
+        startReaderThread();
         setConnected(true);
         logger.debug("Simulated connection opened");
     }