• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Future.cancel(true)

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic