[fix] Put wait in while loops to consider spurious interruptions.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2021-11-07 11:44:20 +01:00
parent 63de5448de
commit 2ece273148
2 changed files with 12 additions and 4 deletions

View File

@ -156,8 +156,11 @@ public abstract class AOutputForwardRunnable implements Runnable {
/** a method to wait some time. */ /** a method to wait some time. */
protected final void waitASec() { protected final void waitASec() {
try { try {
final long tic = System.currentTimeMillis();
synchronized (this) { synchronized (this) {
wait(timeout); while (System.currentTimeMillis() - tic < timeout) {
wait(timeout + tic - System.currentTimeMillis());
}
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$ LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$

View File

@ -128,9 +128,12 @@ public final class ReadingRunnable implements Runnable {
* @throws IOException if the runnable was stopped and no essage was found. */ * @throws IOException if the runnable was stopped and no essage was found. */
private void doWaitMessage(final long timeout) throws IOException { private void doWaitMessage(final long timeout) throws IOException {
try { try {
final long tic = System.currentTimeMillis();
synchronized (lock) { synchronized (lock) {
while (System.currentTimeMillis() - tic < timeout && messages.isEmpty()) {
lock.wait(timeout); lock.wait(timeout);
} }
}
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e); LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
@ -205,9 +208,11 @@ public final class ReadingRunnable implements Runnable {
mLock = messageBlocker.get(message); mLock = messageBlocker.get(message);
} }
synchronized (mLock) { synchronized (mLock) {
while (messageBlocker.containsKey(message)) {
mLock.wait(); mLock.wait();
} }
} }
}
/** Test if some data is available. /** Test if some data is available.
* *
@ -251,9 +256,9 @@ public final class ReadingRunnable implements Runnable {
final Object mLock = messageBlocker.get(message); final Object mLock = messageBlocker.get(message);
if (mLock!=null) { if (mLock!=null) {
synchronized (mLock) { synchronized (mLock) {
messageBlocker.remove(message);
mLock.notifyAll(); mLock.notifyAll();
} }
messageBlocker.remove(message);
} }
} }
} }