[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:
lolodomo 2023-01-27 13:23:04 +01:00 committed by GitHub
parent 865c4b8e86
commit 83c20b0bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 10 deletions

View 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 {

View 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;

View File

@ -94,7 +94,7 @@ public class RotelSerialConnector extends RotelConnector {
}
}
readerThread.start();
startReaderThread();
this.serialPort = commPort;
this.dataIn = dataIn;

View 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");
}