• Post Reply Bookmark Topic Watch Topic
  • New Topic

Future.cancel(true)  RSS feed

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a Callable object and submitted it to a task pool. Inside the callable method, I check Thread.currentThread().isInterrupted(). However, when I call Future.cancel(true), Thread.currentThread().isInterrupted() never returns true.

Am I checking for interruptions incorrectly?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be good if you show us the code.

javadocs wrote: If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.


Did your task start when you cancelled it?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. It starts and then waits for a thread interrupt. Nothing ever comes. And none of the threads ever cancel before they start.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bai Shen wrote:Yup. It starts and then waits for a thread interrupt. Nothing ever comes. And none of the threads ever cancel before they start.


How do you 'wait for the interrupt? Usually, methods that throw an InterruptedException will reset the interrupted flag (meaning the thread's isInterrupted() method returns false after catching an InterruptedException). So using wait() or sleep() and catching the InterruptedException would essentially make sure the isInterrupted() flag is not true.

You would have to re-set it in the catch block if you want the interrupted state to be seen:
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried several methods. I don't remember them all now, unfortunately.

But no matter what I did, it didn't see to get to the thread.

Right now I just let the thread complete. It's wasting resources, but I haven't had time to devote to figuring out a fix.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!