|
44 | 44 | * <p> |
45 | 45 | * How to use it:<br> |
46 | 46 | * 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. |
50 | 52 | * |
51 | 53 | * @author trytocatch@163.com |
52 | 54 | * @date 2013-2-2 |
@@ -147,7 +149,7 @@ public void run() { |
147 | 149 | barrier.nextCycle(workingVersion); |
148 | 150 | } catch (Throwable t) { |
149 | 151 | t.printStackTrace(); |
150 | | - workState.set(WS_CANCELED); |
| 152 | + fireCalculationFailed(); |
151 | 153 | } |
152 | 154 | } |
153 | 155 | } catch (InterruptedException e) { |
@@ -184,10 +186,6 @@ public int getDelayUpperLimit() { |
184 | 186 | public void setDelayUpperLimit(int delayUpperLimit) { |
185 | 187 | this.delayUpperLimit = delayUpperLimit; |
186 | 188 | } |
187 | | - |
188 | | - public final void stopCurrentWorking() { |
189 | | - workState.set(WS_CANCELED); |
190 | | - } |
191 | 189 |
|
192 | 190 | /** |
193 | 191 | * |
@@ -251,8 +249,22 @@ protected final void updateParametersVersion() { |
251 | 249 | workState.compareAndSet(WS_WORKING, WS_NEW_TASK); |
252 | 250 | } |
253 | 251 |
|
| 252 | + public final void stopCurrentWorking() { |
| 253 | + workState.set(WS_CANCELED); |
| 254 | + } |
| 255 | + |
254 | 256 | /** |
255 | 257 | * implement this to deal with you task |
256 | 258 | */ |
257 | 259 | 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 | + } |
258 | 270 | } |
0 commit comments