[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 Logger logger = LoggerFactory.getLogger(RotelConnector.class);
|
||||||
|
|
||||||
|
private final RotelAbstractProtocolHandler protocolHandler;
|
||||||
private final boolean simu;
|
private final boolean simu;
|
||||||
protected final Thread readerThread;
|
private final String readerThreadName;
|
||||||
|
|
||||||
|
private @Nullable Thread readerThread;
|
||||||
|
|
||||||
/** The output stream */
|
/** The output stream */
|
||||||
protected @Nullable OutputStream dataOut;
|
protected @Nullable OutputStream dataOut;
|
||||||
|
@ -54,8 +57,9 @@ public abstract class RotelConnector {
|
||||||
* @param readerThreadName the name of thread to be created
|
* @param readerThreadName the name of thread to be created
|
||||||
*/
|
*/
|
||||||
public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
|
public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
|
||||||
|
this.protocolHandler = protocolHandler;
|
||||||
this.simu = simu;
|
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();
|
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
|
* Stop the thread that handles the feedback messages and close the opened input and output streams
|
||||||
*/
|
*/
|
||||||
protected void cleanup() {
|
protected void cleanup() {
|
||||||
readerThread.interrupt();
|
stopReaderThread();
|
||||||
try {
|
|
||||||
readerThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
OutputStream dataOut = this.dataOut;
|
OutputStream dataOut = this.dataOut;
|
||||||
if (dataOut != null) {
|
if (dataOut != null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -68,7 +68,7 @@ public class RotelIpConnector extends RotelConnector {
|
||||||
dataOut = new DataOutputStream(clientSocket.getOutputStream());
|
dataOut = new DataOutputStream(clientSocket.getOutputStream());
|
||||||
dataIn = new DataInputStream(clientSocket.getInputStream());
|
dataIn = new DataInputStream(clientSocket.getInputStream());
|
||||||
|
|
||||||
readerThread.start();
|
startReaderThread();
|
||||||
|
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class RotelSerialConnector extends RotelConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readerThread.start();
|
startReaderThread();
|
||||||
|
|
||||||
this.serialPort = commPort;
|
this.serialPort = commPort;
|
||||||
this.dataIn = dataIn;
|
this.dataIn = dataIn;
|
||||||
|
|
|
@ -132,7 +132,7 @@ public class RotelSimuConnector extends RotelConnector {
|
||||||
@Override
|
@Override
|
||||||
public synchronized void open() throws RotelException {
|
public synchronized void open() throws RotelException {
|
||||||
logger.debug("Opening simulated connection");
|
logger.debug("Opening simulated connection");
|
||||||
readerThread.start();
|
startReaderThread();
|
||||||
setConnected(true);
|
setConnected(true);
|
||||||
logger.debug("Simulated connection opened");
|
logger.debug("Simulated connection opened");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue