java TimerTask增加时间?

发布于 2024-11-25 09:20:07 字数 372 浏览 7 评论 0原文

您好,我使用以下计时器任务,我想在发生某种情况时增加此任务的时间,

Timer timer2=new Timer();                   
                timer2.schedule(new TimerTask(){
                    public void run(){

                        //whatevr
                    }
                }, 4000);

例如

if(mycondition)

{
increase time????
}

我该怎么做

Hi m using the following timer task,and i want to increase the time of this task when a certain condition occurs

Timer timer2=new Timer();                   
                timer2.schedule(new TimerTask(){
                    public void run(){

                        //whatevr
                    }
                }, 4000);

examlpe

if(mycondition)

{
increase time????
}

how can i do that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待我真够勒 2024-12-02 09:20:07

TimerTask 提取到内部类或独立类中。取消当前正在运行的计时器任务并安排一个增加时间段的新实例。

Extract the TimerTask in an inner or standalone class. Cancel currently running timer task and schedule a new instance with increased time period.

旧伤还要旧人安 2024-12-02 09:20:07

你不能。您必须在增加的时间段内安排一项新任务。如果上一个任务已过时,请确保您 cancel() 它。


为了便于将来参考,我建议您使用 Executors 框架。

You can't. You'll have to schedule a new task with the incremented period. And if the previous task has become obsolete, make sure that you cancel() it.


For future reference, I recommend you utilize the Executors framework.

迷雾森÷林ヴ 2024-12-02 09:20:07

如有必要,从 run() 提交另一项任务:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;



public class TimerTaskTest {

private static class MyTimerTask extends TimerTask {
    private final Timer timer;
    private boolean fire;

    private MyTimerTask(Timer timer) {
        this(timer, false);
    }

    private MyTimerTask(Timer timer, boolean fire) {
        this.timer = timer;
        this.fire = fire;
    }

    @Override
    public void run() {
        if (!fire) {
            System.out.println(new Date() + " - steady...");
            timer.schedule(new MyTimerTask(timer, true), 2000);
        } else {
            System.out.println(new Date() + " - go!");
        }
    }
}

public static void main(String args[]) {
    Timer timer = new Timer(true);
    MyTimerTask timerTask = new MyTimerTask(timer);

    System.out.println(new Date() + " - ready...");
    timer.schedule(timerTask, 4000);

    try {
        Thread.sleep(7000);
    } catch (Exception ignore) {
    }
}

}

Submit another one task from run() if necessary:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;



public class TimerTaskTest {

private static class MyTimerTask extends TimerTask {
    private final Timer timer;
    private boolean fire;

    private MyTimerTask(Timer timer) {
        this(timer, false);
    }

    private MyTimerTask(Timer timer, boolean fire) {
        this.timer = timer;
        this.fire = fire;
    }

    @Override
    public void run() {
        if (!fire) {
            System.out.println(new Date() + " - steady...");
            timer.schedule(new MyTimerTask(timer, true), 2000);
        } else {
            System.out.println(new Date() + " - go!");
        }
    }
}

public static void main(String args[]) {
    Timer timer = new Timer(true);
    MyTimerTask timerTask = new MyTimerTask(timer);

    System.out.println(new Date() + " - ready...");
    timer.schedule(timerTask, 4000);

    try {
        Thread.sleep(7000);
    } catch (Exception ignore) {
    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文