Synchronization organisation
This commit is contained in:
parent
98d22782c1
commit
761d640f0b
@ -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()) {
|
||||||
|
@ -88,18 +88,18 @@ import java.util.logging.Logger;
|
|||||||
public final class WritingRunnable implements Runnable {
|
public final class WritingRunnable implements Runnable {
|
||||||
|
|
||||||
/** Wait timeout. */
|
/** Wait timeout. */
|
||||||
private static final long TIMEOUT = 1000;
|
private static final long TIMEOUT = 1000;
|
||||||
/** Class logger. */
|
/** Class logger. */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(WritingRunnable.class.getName());
|
.getLogger(WritingRunnable.class.getName());
|
||||||
/** Messages to write. */
|
/** Messages to write. */
|
||||||
private final Deque<String> messages = new ArrayDeque<>();
|
private final Deque<String> messages = new ArrayDeque<>();
|
||||||
/** Stream to write to. */
|
/** Stream to write to. */
|
||||||
private final OutputStream outPrint;
|
private final OutputStream outPrint;
|
||||||
/** The charset. */
|
/** The charset. */
|
||||||
private final Charset charset;
|
private final Charset charset;
|
||||||
/** Runnable state. */
|
/** Runnable state. */
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
|
|
||||||
/** Synchro object. */
|
/** Synchro object. */
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@ -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 {
|
||||||
lock.wait(TIMEOUT);
|
synchronized (lock) {
|
||||||
|
while (running && messages.isEmpty()) {
|
||||||
|
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());
|
||||||
|
Loading…
Reference in New Issue
Block a user