private final String[] mQueryRegExp;
private final ConcurrentAsyncResultListener mAsyncResultListener;
- private boolean mCancelled;
- private boolean mEnded;
+ private volatile boolean mCancelled;
+ private volatile boolean mEnded;
private final Object mSync = new Object();
/**
findRecursive(f);
}
+ // record program's execution termination
+ synchronized (mSync) {
+ mEnded = true;
+ // account for the delay between the end of findRecursive and setting program
+ // termination flag (mEnded)
+ // notify again in case a thread entered wait state since
+ mSync.notify();
+ }
+
if (this.mAsyncResultListener != null) {
this.mAsyncResultListener.onAsyncEnd(this.mCancelled);
}
public boolean cancel() {
try {
synchronized (this.mSync) {
- this.mCancelled = true;
- this.mSync.wait(5000L);
+ // ensure the program is running before attempting to cancel
+ // there won't be a corresponding lock.notify() otherwise
+ if (!mEnded) {
+ this.mCancelled = true;
+ this.mSync.wait(5000L);
+ }
}
} catch (Exception e) {/**NON BLOCK**/}
return true;
public boolean end() {
try {
synchronized (this.mSync) {
- this.mEnded = true;
- this.mSync.wait(5000L);
+ // ensure the program is running before attempting to terminate
+ if (!mEnded) {
+ this.mEnded = true;
+ this.mSync.wait(5000L);
+ }
}
} catch (Exception e) {/**NON BLOCK**/}
return true;