vsilaev / tascalate-concurrent

Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OnTimeout not working when computing intensive task

msche opened this issue · comments

I have a computing intensive task that should stop when a certain time has expired. I'm using the onTimeout method for this but the task is not interrupted. When i add a thread sleep in the computation task the task is properly interrupted.

I'm using a ThreadPoolExecutor as executer.

Any idea what could be the cause of this?

Please create a test that show-case an issue.
But from what I read above, it looks like there is no point where computational task is ever could be interrupted -- i.e. no blocking calls inside. When you add Thread.sleep then you are effectively adding a blocking call. Please try to check Thread.interrupted flag periodically.
So all in all, it seems to be not an issue -- unless you have no blocking calls you can't interrupt a task. Sure, unless you prove the opposite with failing test,

Thanks for the quick response. I will include the interrupted() check within my task and see whether that fixes the (non) issue. If i still see the same behavior I will try to write a test.

Probably the term "non-issue" is not correct, but I just want to highlight a fact, that the library expects certain cooperative behavior from the user code: either there are some blocking calls inside or user code checks for interruption status. My apologies if the original wording was not polite.
Just to recall, the correct sequence to check interruption status is:

if (Thread.interruted()) {
  Thread.currentThread().interrupt(); // set flag while it's cleared by check
  return;
}

Or simply:

if (Thread.currentThread().isInterrupted()) return;

yborovikov,

Yes this is correct option too.

I'm closing the issue while nothing besides the suggested solution is possible in this case (no changes to the library are necessary/planned)