I have been checking similar questions. This one is quite similar what I want to ask but not really I have the answer. Also This question I have checked.
Question that I have is that I have a SwingWorker
which does long processes in the background.
@Override
protected final List<Object> doInBackground() throws Exception
{
//Should I add if(this.isCancelled) here?
List<Object> objectList = someFecthingMethod();
//Should I add if(this.isCancelled) here?
objectList.forEach(object -> executor.submit(new Helper(object));
this.latch.await();
//Should I add if(this.isCancelled) here?
someOtherLongRunningMethodBasedOnHelperResults();
}
In scratch I have a code like that. This runs when calculate button of mine which triggers the worker when it is clicked. I also want to be able to cancel it. I have written needed method stop()
where it has cancel()
method of worker itself.
Like below in my Worker Class I have my stop method like below:
@Override
public boolean stop()
{
return this.cancel( true );
}
When I call this method, I check in doInBackground()
, this.isCancelled()
returns true.
. But anyway it keeps executing the methods within doInBackground().
So my question is that, should I be adding if(this.isCancelled())
check before every method in doInBackground()
to stop it immediately, is there any better way to do stop it from executing when its canceled?
Thank you in advance.