• 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

Understanding of the ScheduledFuture Interface

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone help me understand the ScheduledFuture interface available in the java.util.concurrent package. I have a class (task) implementing the Runnable interface which I schedule using a ScheduledExecutorService which in turn returns me an object of the ScheduledFuture.

My Query is that if I call cancel method on the returned ScheduledFuture object, will it cancel the actual runnable class or my understanding is wrong or this is not a correct way of scheduling a Runnable?

Also, if I am doing it correctly then can somebody explain how it works under the wraps.

Thanks in advance for help!!!
 
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
The cancel() is used for two reasons:
1. Prevent a task that is already scheduled from actually running. This works if the canel() method is called prior to the task starting.
2. Stop a task that is already running. If the task has already begun, and the boolean mayInterruptIfRunning flag is false, then it lets the current task complete. If mayInterruptIfRunning is true, then it attempts to interrupt the Thread running the task, to cause it to end. It will call the interrupt() on the task Thread. In order for it to work you task has to respond to interruption, for example by periodically checking Thread.interrupted() to see if it should end.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic