Skip to content

Commit affab9f

Browse files
committed
fix bug
'stopCurrentWorking' may cause new calculation be cancelled, so create a new method 'fireCalculationFailed' to avoid that problem.
1 parent f857713 commit affab9f

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/trytocatch/regexreplacer/model/RegexController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected void calculateResult() {
183183
try {
184184
workOutResultImpl();
185185
} catch (Throwable t) {
186-
stopCurrentWorking();
186+
fireCalculationFailed();
187187
if (resultObserver != null)
188188
resultObserver.onResultChanged(
189189
null,

src/trytocatch/utils/concurrent/LatestResultsProvider.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444
* <p>
4545
* How to use it:<br>
4646
* Implement the abstract method 'calculateResult' to do your calculation with your data,
47-
* cancel your calculation if you detect that the method 'isWorking' returns false, call
48-
* the method 'updateParametersVersion' once the data has changed, call the method
49-
* 'update' or 'updateAndWait' to launch the calculation if you want to get the result.
47+
* call 'fireCalculationFailed' while calculation failed and no Throwalbe be thrown by
48+
* 'calculateResult', cancel your calculation if you detect that the method 'isWorking'
49+
* returns false, call the method 'updateParametersVersion' once the data has changed,
50+
* call the method 'update' or 'updateAndWait' to launch the calculation if you want to
51+
* get the result.
5052
*
5153
* @author trytocatch@163.com
5254
* @date 2013-2-2
@@ -147,7 +149,7 @@ public void run() {
147149
barrier.nextCycle(workingVersion);
148150
} catch (Throwable t) {
149151
t.printStackTrace();
150-
workState.set(WS_CANCELED);
152+
fireCalculationFailed();
151153
}
152154
}
153155
} catch (InterruptedException e) {
@@ -184,10 +186,6 @@ public int getDelayUpperLimit() {
184186
public void setDelayUpperLimit(int delayUpperLimit) {
185187
this.delayUpperLimit = delayUpperLimit;
186188
}
187-
188-
public final void stopCurrentWorking() {
189-
workState.set(WS_CANCELED);
190-
}
191189

192190
/**
193191
*
@@ -251,8 +249,22 @@ protected final void updateParametersVersion() {
251249
workState.compareAndSet(WS_WORKING, WS_NEW_TASK);
252250
}
253251

252+
public final void stopCurrentWorking() {
253+
workState.set(WS_CANCELED);
254+
}
255+
254256
/**
255257
* implement this to deal with you task
256258
*/
257259
protected abstract void calculateResult();
260+
261+
/**
262+
* If 'calculate the result' failed and no Throwable be thrown by
263+
* 'calculateResult', calls this to let it know so that isResultUptodate()
264+
* won't become true. If a Throwable be thrown by 'calculateResult', this
265+
* tool will catch it and call 'fireCalculationFailed'.
266+
*/
267+
public final void fireCalculationFailed() {
268+
workState.compareAndSet(WS_WORKING, WS_CANCELED);
269+
}
258270
}

0 commit comments

Comments
 (0)