Synchronization organisation

This commit is contained in:
Emmanuel Bigeon 2018-10-11 12:09:24 -04:00
parent 98d22782c1
commit 761d640f0b
2 changed files with 17 additions and 15 deletions

View File

@ -265,6 +265,7 @@ public final class ReadingRunnable implements Runnable {
message); message);
final Thread th = new Thread(waitRunn); final Thread th = new Thread(waitRunn);
// Wait for the thread to actually start before unlocking the message queue.
synchronized (start) { synchronized (start) {
th.start(); th.start();
while (!waitRunn.isStarted()) { while (!waitRunn.isStarted()) {

View File

@ -143,15 +143,14 @@ public final class WritingRunnable implements Runnable {
public void run() { public void run() {
while (running) { while (running) {
synchronized (lock) { synchronized (lock) {
while (messages.isEmpty()) {
waitNextMessage(); waitNextMessage();
if (!running) { if (!running) {
return; return;
} }
}
writeMessage(); writeMessage();
} }
} }
} }
/** Set the running status. /** Set the running status.
@ -166,11 +165,14 @@ public final class WritingRunnable implements Runnable {
/** Wait for next message. */ /** Wait for next message. */
private void waitNextMessage() { private void waitNextMessage() {
try { try {
synchronized (lock) {
while (running && messages.isEmpty()) {
lock.wait(TIMEOUT); lock.wait(TIMEOUT);
}
}
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
if (running) { if (running) {
LOGGER.log(Level.SEVERE, LOGGER.log(Level.SEVERE, "Thread interruption exception.", e); //$NON-NLS-1$
"Thread interruption exception.", e); //$NON-NLS-1$
} }
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -179,8 +181,7 @@ public final class WritingRunnable implements Runnable {
/** Write next message to output. */ /** Write next message to output. */
private void writeMessage() { private void writeMessage() {
final String message = messages.poll(); final String message = messages.poll();
final ByteBuffer buff = charset final ByteBuffer buff = charset.encode(message + System.lineSeparator());
.encode(message + System.lineSeparator());
if (buff.hasArray()) { if (buff.hasArray()) {
try { try {
outPrint.write(buff.array()); outPrint.write(buff.array());