Review of solution for Cancelled Tasks

Tom Eugelink tbee at tbee.org
Thu Jan 5 13:53:09 PST 2012


On 2012-01-05 22:21, Roman Kennke wrote:
> new Task() {
>      public Object call() throws Exception {
>          for (int i=0; i<100; i++) {
>              if (isCancelled()) break;
>              try {
>                  doSomething();
>                  updateProgress(i, 100);
>               } catch (Exception e) { }
>          }
>          return null;
>      }
>
>      @Override protected void cancelled() {
>          updateMessage("Cancelled");
>      }
> };
>
> The cancelled() method would be called as soon as the task gets
> cancelled, where the application can perform the necessary cleanup, call
> updateProgress() or whatever without blowing up.
>
> Maybe this should be combined with throwing the TaskCancelledException
> (but only *after* the above call to cancelled() returned) to signal that
> the task can stop doing whatever it is doing, and roll up the executing
> thread.
>

I must admit that I've lost the discussion somewhere.

But seeing the code above, with the if(isCancelled()) check to break the loop, I do not see any need to throw an exception. The Task, after returing from call(), can check itself for isCancelled() and call the cancelled() method in the EDT (or whatever) for update. So, why an exception?

Tom



More information about the openjfx-dev mailing list