mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[rotel] Fix reader thread handling (#14272)
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>
This commit is contained in:
parent
865c4b8e86
commit
83c20b0bd6
@ -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 {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -94,7 +94,7 @@ public class RotelSerialConnector extends RotelConnector {
|
||||
}
|
||||
}
|
||||
|
||||
readerThread.start();
|
||||
startReaderThread();
|
||||
|
||||
this.serialPort = commPort;
|
||||
this.dataIn = dataIn;
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user