• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding of the ScheduledFuture Interface

 
Jigs Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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!!!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!