simplify PausableThread
- make pausing work properly
This commit is contained in:
parent
33c645e888
commit
26cb2e6603
@ -25,43 +25,42 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public abstract class PausableThread extends Thread {
|
public abstract class PausableThread extends Thread {
|
||||||
private final static Logger log = LoggerFactory.getLogger(PausableThread.class);
|
private final static Logger log = LoggerFactory.getLogger(PausableThread.class);
|
||||||
private final static boolean DEBUG = false;
|
private final static boolean dbg = false;
|
||||||
|
|
||||||
private boolean mPausing = true;
|
private boolean mPausing = true;
|
||||||
|
private boolean mRunning = true;
|
||||||
private boolean mShouldPause = false;
|
private boolean mShouldPause = false;
|
||||||
|
private boolean mShouldStop = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Causes the current thread to wait until this thread is pausing.
|
* Causes the current thread to wait until this thread is pausing.
|
||||||
*/
|
*/
|
||||||
public final void awaitPausing() {
|
public final void awaitPausing() {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
while (!isInterrupted() && !isPausing()) {
|
|
||||||
if (DEBUG)
|
while (!isPausing()) {
|
||||||
log.debug("await {}", getThreadName());
|
|
||||||
|
if (dbg)
|
||||||
|
log.debug("Await Pause {}", getThreadName());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wait(10);
|
wait(100);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// restore the interrupted status
|
/* restore the interrupted status */
|
||||||
Thread.currentThread().interrupt();
|
this.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public synchronized void finish() {
|
||||||
public void interrupt() {
|
if (!mRunning)
|
||||||
if (DEBUG)
|
return;
|
||||||
log.debug("interrupt {}", getThreadName());
|
|
||||||
|
|
||||||
// first acquire the monitor which is used to call wait()
|
log.debug("Finish {}", getThreadName());
|
||||||
synchronized (this) {
|
|
||||||
super.interrupt();
|
mShouldStop = true;
|
||||||
}
|
this.interrupt();
|
||||||
//try {
|
|
||||||
// join();
|
|
||||||
//} catch (InterruptedException e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,14 +76,10 @@ public abstract class PausableThread extends Thread {
|
|||||||
public final synchronized void pause() {
|
public final synchronized void pause() {
|
||||||
if (!mShouldPause) {
|
if (!mShouldPause) {
|
||||||
mShouldPause = true;
|
mShouldPause = true;
|
||||||
notify();
|
this.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final synchronized boolean isCanceled() {
|
|
||||||
return mShouldPause;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The paused thread should continue with its work.
|
* The paused thread should continue with its work.
|
||||||
*/
|
*/
|
||||||
@ -95,22 +90,43 @@ public abstract class PausableThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final synchronized boolean isCanceled() {
|
||||||
|
return mShouldPause;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void run() {
|
public final void run() {
|
||||||
|
mRunning = true;
|
||||||
setName(getThreadName());
|
setName(getThreadName());
|
||||||
setPriority(getThreadPriority());
|
setPriority(getThreadPriority());
|
||||||
|
|
||||||
while (!isInterrupted()) {
|
O: while (!mShouldStop) {
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
while (!isInterrupted() && (mShouldPause || !hasWork())) {
|
if (mShouldStop)
|
||||||
|
break;
|
||||||
|
|
||||||
|
while ((mShouldPause || !hasWork())) {
|
||||||
try {
|
try {
|
||||||
if (mShouldPause) {
|
if (mShouldPause) {
|
||||||
mPausing = true;
|
mPausing = true;
|
||||||
|
|
||||||
|
if (dbg)
|
||||||
|
log.debug("Pausing: {}",
|
||||||
|
getThreadName());
|
||||||
}
|
}
|
||||||
|
|
||||||
wait();
|
wait();
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// restore the interrupted status
|
if (dbg)
|
||||||
interrupt();
|
log.debug("Interrupted {} {}:{}",
|
||||||
|
getThreadName(),
|
||||||
|
mShouldPause,
|
||||||
|
mShouldStop);
|
||||||
|
|
||||||
|
if (mShouldStop)
|
||||||
|
break O;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,27 +134,24 @@ public abstract class PausableThread extends Thread {
|
|||||||
mPausing = false;
|
mPausing = false;
|
||||||
afterPause();
|
afterPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG)
|
|
||||||
log.debug("resume {}", getThreadName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isInterrupted()) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
doWork();
|
doWork();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// restore the interrupted status
|
if (dbg)
|
||||||
interrupt();
|
log.debug("Interrupted {} {}:{}",
|
||||||
|
getThreadName(),
|
||||||
|
mShouldPause,
|
||||||
|
mShouldStop);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG)
|
log.debug("Done {}", getThreadName());
|
||||||
log.debug("finish {}", getThreadName());
|
|
||||||
|
|
||||||
mPausing = true;
|
mPausing = true;
|
||||||
|
mRunning = false;
|
||||||
|
|
||||||
afterRun();
|
afterRun();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user