Class A
{
long x;
method1()
{
x = current time in millisecs;
}
task()//want to run this after (x+30) time
}
I need to run task() after (x+30) . x could be varying. if method1 is called, then task is scheduled to run after 30 from current time, but within that 30 timeperiod if method1 is called again then i want to cancel the previous task call and want to schedule a new call to task after 30 sec from current time. How should i create a scheduler or task of this type?
Went through the scheduledthreadpoolexecutor API but didn’t find a scheduler of this type.
You’re asking 2 questions:
1. How can I schedule a task with an arbitrary delay?
You can use one of the schedule methods on a java.util.concurrent.ScheduledThreadPoolExecutor
2. How can I cancel an already running task?
You cancel a task by calling cancel on the Future that is returned from the
schedulemethod you called.