Reputation: 2293
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.
Upvotes: 3
Views: 1536
Reputation: 32717
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
int delay = System.currentTimeMillis + 30;
myScheduledExecutor.schedule(myTask, delay, TimeUnit.MILLISECONDS);
2. How can I cancel an already running task?
You cancel a task by calling cancel on the Future that is returned from the schedule
method you called.
if (!future.isDone()){
future.cancel(true);
}
future = myScheduledExecutor.schedule(myTask, delay, TimeUnit.MILLISECONDS);
Upvotes: 4
Reputation: 4389
Use java.util.Timer
and pass a callback into the TimerTask
to schedule the next run. TimerTask can be cancelled with cancel
method if needed. e.g.
package test;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTaskDemo {
private Timer timer = new Timer();
private MyTimerTask nextTask = null;
private interface Callback {
public void scheduleNext(long delay);
}
Callback callback = new Callback() {
@Override
public void scheduleNext(long delay) {
nextTask = new MyTimerTask(this);
timer.schedule(nextTask, delay);
}
};
public static class MyTimerTask extends TimerTask {
Callback callback;
public MyTimerTask(Callback callback) {
this.callback = callback;
}
@Override
public void run() {
// You task code
int delay = 1000;
callback.scheduleNext(delay);
};
}
public void start() {
nextTask = new MyTimerTask(callback);
timer.schedule(nextTask, 1000);
}
public static void main(String[] args) {
new TimerTaskDemo().start();
}
}
Upvotes: 1
Reputation: 2549
I think the easiest way to do what you need is the following. Class B
is the calling class.
class A {
public void runAfterDelay(long timeToWait) throws InterruptedException {
Thread.sleep(timeToWait);
task();
}
}
class B {
public static void main(String[] args) throws InterruptedException {
A a = new A();
// run after 30 seconds
a.runAfterDelay(30000);
}
}
Upvotes: 0
Reputation: 2783
Class A
{
$x;
function method1()
{
$time = microtime(true);
}
sleep($time + 30);
task()//want to run this after (x+30) time
}
Upvotes: -1
Reputation: 3191
Why don't you model your requirement using the Timer class of the JDK. Based on your requirements you will be scheduling the task in the timer as required.
Upvotes: 0
Reputation: 533870
I would record the time method1 is called and I would check every second whether the method was called 30 seconds ago. This way it will only perform the task when there has been no call for 30 seconds.
Upvotes: 1