From 2de65767658794647fdc619e6bb6aee4e3ae44a3 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sun, 19 Jan 2014 14:51:17 +0100 Subject: [PATCH] fix PausableThread: set 'mPausing = false' on thread after pause, not on call to proceed() as the thread may already have been stopped in which case awaitPausing will loop forever -- short: dont use thread utility classes you havent written yourself :) --- vtm/src/org/oscim/utils/PausableThread.java | 41 +++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/vtm/src/org/oscim/utils/PausableThread.java b/vtm/src/org/oscim/utils/PausableThread.java index 19a75d7a..cb4641b7 100644 --- a/vtm/src/org/oscim/utils/PausableThread.java +++ b/vtm/src/org/oscim/utils/PausableThread.java @@ -17,12 +17,18 @@ */ package org.oscim.utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * An abstract base class for threads which support pausing and resuming. */ public abstract class PausableThread extends Thread { + private final static Logger log = LoggerFactory.getLogger(PausableThread.class); + private final static boolean DEBUG = false; + private boolean mPausing = true; - private boolean mShouldPause; + private boolean mShouldPause = false; /** * Causes the current thread to wait until this thread is pausing. @@ -30,8 +36,10 @@ public abstract class PausableThread extends Thread { public final void awaitPausing() { synchronized (this) { while (!isInterrupted() && !isPausing()) { + if (DEBUG) + log.debug("await {}", getThreadName()); try { - wait(100); + wait(10); } catch (InterruptedException e) { // restore the interrupted status Thread.currentThread().interrupt(); @@ -42,17 +50,13 @@ public abstract class PausableThread extends Thread { @Override public void interrupt() { + if (DEBUG) + log.debug("interrupt {}", getThreadName()); + // first acquire the monitor which is used to call wait() synchronized (this) { super.interrupt(); } - - //try { - // this.join(10000); - //} catch (InterruptedException e) { - // // restore the interrupted status - // Thread.currentThread().interrupt(); - //} } /** @@ -72,14 +76,16 @@ public abstract class PausableThread extends Thread { } } + public final synchronized boolean isCanceled() { + return mShouldPause; + } + /** * The paused thread should continue with its work. */ public final synchronized void proceed() { if (mShouldPause) { mShouldPause = false; - mPausing = false; - afterPause(); notify(); } } @@ -102,6 +108,14 @@ public abstract class PausableThread extends Thread { interrupt(); } } + + if (mPausing) { + mPausing = false; + afterPause(); + } + + if (DEBUG) + log.debug("resume {}", getThreadName()); } if (isInterrupted()) { @@ -116,6 +130,11 @@ public abstract class PausableThread extends Thread { } } + if (DEBUG) + log.debug("finish {}", getThreadName()); + + mPausing = true; + afterRun(); }