Factor constant string and comments added to ReadingRunnable

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2016-11-30 20:07:28 -05:00
parent e602a269f8
commit d4f428d311

View File

@ -51,6 +51,8 @@ import java.util.logging.Logger;
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class ReadingRunnable implements Runnable { public class ReadingRunnable implements Runnable {
/** The closed pipe message */
private static final String CLOSED_PIPE = "Closed pipe"; //$NON-NLS-1$
/** Wait timeout */ /** Wait timeout */
private static final long TIMEOUT = 1000; private static final long TIMEOUT = 1000;
/** Class logger */ /** Class logger */
@ -65,6 +67,7 @@ public class ReadingRunnable implements Runnable {
/** Synchro object */ /** Synchro object */
private final Object lock = new Object(); private final Object lock = new Object();
/** The waiting status for a message */
private boolean waiting; private boolean waiting;
/** @param reader the input to read from */ /** @param reader the input to read from */
@ -122,7 +125,7 @@ public class ReadingRunnable implements Runnable {
public String getMessage() throws IOException { public String getMessage() throws IOException {
synchronized (lock) { synchronized (lock) {
if (!running) { if (!running) {
throw new IOException("Closed pipe"); //$NON-NLS-1$ throw new IOException(CLOSED_PIPE);
} }
waiting = true; waiting = true;
while (messages.isEmpty()) { while (messages.isEmpty()) {
@ -133,7 +136,7 @@ public class ReadingRunnable implements Runnable {
e); e);
} }
if (messages.isEmpty() && !running) { if (messages.isEmpty() && !running) {
throw new IOException("Closed pipe"); //$NON-NLS-1$ throw new IOException(CLOSED_PIPE);
} }
} }
LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$ LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$
@ -161,19 +164,17 @@ public class ReadingRunnable implements Runnable {
public boolean hasMessage() throws IOException { public boolean hasMessage() throws IOException {
synchronized (lock) { synchronized (lock) {
if (!running) { if (!running) {
throw new IOException("Closed pipe"); //$NON-NLS-1$ throw new IOException(CLOSED_PIPE);
} }
return !messages.isEmpty(); return !messages.isEmpty();
} }
} }
/** /** Interrupts the wait on the next message by providing an empty message */
*
*/
public void interrupt() { public void interrupt() {
synchronized (lock) { synchronized (lock) {
if (waiting) { if (waiting) {
messages.offer(""); messages.offer(""); //$NON-NLS-1$
lock.notify(); lock.notify();
} }
} }