Fixed run lockings

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-11 12:42:14 -04:00
parent f7876dc964
commit 159805701c
2 changed files with 24 additions and 11 deletions

View File

@ -155,7 +155,7 @@ public final class ReadingRunnable implements Runnable {
.getLogger(ReadingRunnable.class.getName()); .getLogger(ReadingRunnable.class.getName());
/** Read messages. */ /** Read messages. */
private final Deque<String> messages = new ArrayDeque<>(); private final Deque<String> messages = new ArrayDeque<>();
/** the reader. */ /** the reader. */
private final BufferedReader reader; private final BufferedReader reader;
@ -170,7 +170,7 @@ public final class ReadingRunnable implements Runnable {
/** The lock. */ /** The lock. */
private final Object messageBlockerLock = new Object(); private final Object messageBlockerLock = new Object();
/** The message being delivered. */ /** The message being delivered. */
private String delivering; private String delivering;
/** Create a reading runnable. /** Create a reading runnable.
* *
@ -320,12 +320,19 @@ public final class ReadingRunnable implements Runnable {
* @see java.lang.Runnable#run() */ * @see java.lang.Runnable#run() */
@Override @Override
public void run() { public void run() {
while (running) { boolean cont = true;
while (cont) {
synchronized (lock) {
cont = running;
if (!running) {
break;
}
}
try { try {
String line = reader.readLine(); String line = reader.readLine();
if (line == null) { if (line == null) {
// Buffer end // Buffer end
running = false; setRunning(false);
return; return;
} }
LOGGER.finer("Read: " + line); //$NON-NLS-1$ LOGGER.finer("Read: " + line); //$NON-NLS-1$
@ -335,17 +342,21 @@ public final class ReadingRunnable implements Runnable {
lock.notifyAll(); lock.notifyAll();
} }
} catch (final InterruptedIOException e) { } catch (final InterruptedIOException e) {
if (running) { synchronized (lock) {
LOGGER.info("Reading interrupted"); //$NON-NLS-1$ if (running) {
LOGGER.info("Reading interrupted"); //$NON-NLS-1$
}
} }
LOGGER.log(Level.FINER, "Read interruption was caused by an exception", //$NON-NLS-1$ LOGGER.log(Level.FINER, "Read interruption was caused by an exception", //$NON-NLS-1$
e); e);
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.log(Level.FINE, "The stream reading threw an exception", //$NON-NLS-1$ LOGGER.log(Level.FINE, "The stream reading threw an exception", //$NON-NLS-1$
e); e);
if (running) { synchronized (lock) {
LOGGER.severe("Unable to read from stream"); //$NON-NLS-1$ if (running) {
running = false; LOGGER.severe("Unable to read from stream"); //$NON-NLS-1$
setRunning(false);
}
} }
return; return;
} }

View File

@ -171,8 +171,10 @@ public final class WritingRunnable implements Runnable {
} }
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
if (running) { synchronized (lock) {
LOGGER.log(Level.SEVERE, "Thread interruption exception.", e); //$NON-NLS-1$ if (running) {
LOGGER.log(Level.SEVERE, "Thread interruption exception.", e); //$NON-NLS-1$
}
} }
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }