简体   繁体   English

如何在变化的时间后执行任务

[英]how to execute a task after varying time

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 + 30)之后运行task()。 x could be varying. x可能会有所不同。 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. 如果调用了method1,那么任务被安排在30当前时间之后运行,但是如果再次调用method1,则在30个时间段内,我想取消之前的任务调用,并希望在当前30秒后安排对任务的新调用时间。 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. 通过scheduledthreadpoolexecutor API,但没有找到这种类型的调度程序。

You're asking 2 questions: 你问了两个问题:

1. How can I schedule a task with an arbitrary delay? 1.如何安排任意延迟的任务?

You can use one of the schedule methods on a java.util.concurrent.ScheduledThreadPoolExecutor 您可以在java.util.concurrent.ScheduledThreadPoolExecutor上使用其中一个计划 方法

int delay = System.currentTimeMillis + 30;
myScheduledExecutor.schedule(myTask, delay, TimeUnit.MILLISECONDS);

2. How can I cancel an already running task? 2.如何取消已经运行的任务?

You cancel a task by calling cancel on the Future that is returned from the schedule method you called. 您可以通过调用取消任务取消未来是从返回的schedule ,你调用的方法。

if (!future.isDone()){
    future.cancel(true);
}
future = myScheduledExecutor.schedule(myTask, delay, TimeUnit.MILLISECONDS);

I would record the time method1 is called and I would check every second whether the method was called 30 seconds ago. 我会记录time1被调用的时间,我会每秒检查该方法是否在30秒前被调用。 This way it will only perform the task when there has been no call for 30 seconds. 这样它只会在没有30秒的呼叫时执行任务。

Use java.util.Timer and pass a callback into the TimerTask to schedule the next run. 使用java.util.Timer并将回调传递给TimerTask以安排下一次运行。 TimerTask can be cancelled with cancel method if needed. 如果需要,可以使用cancel方法取消TimerTask。 eg 例如

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();
    }
}

Why don't you model your requirement using the Timer class of the JDK. 为什么不使用JDK的Timer类来建模您的需求。 Based on your requirements you will be scheduling the task in the timer as required. 根据您的要求,您将根据需要在计时器中安排任务。

I think the easiest way to do what you need is the following. 我认为最简单的方法是做你需要的是以下几点。 Class B is the calling class. B类是调用类。

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);
    }
}
Class A 
{
 $x;
 function method1()
  {
   $time = microtime(true);
  }
 sleep($time + 30);
 task()//want to run this after (x+30) time
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM