且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有代码检查计时器是否正在运行?

更新时间:2022-11-06 17:51:39

我在文档中看不到任何用于检查TimerTask对象状态的文档(

I don't see anything in the documentation that provides for checking the status on a TimerTask object (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) so one option would be to extend TimerTask and create your own class. Instead of using an anonymous TimerTask, you could create something along the lines of:

public class CoresTimerTask extends TimerTask {

    private boolean hasStarted = false;

    @Overrides
    public void run() {
        this.hasStarted = true;
        //rest of run logic here...
    }

    public boolean hasRunStarted() {
        return this.hasStarted;
    }
}

,只需维护对此CoresTimerTask对象的引用,然后将其传递给startTimer().然后,您可以稍后通过hasRunStarted检查该对象.

and just maintain a reference to this CoresTimerTask object, which you then pass into startTimer(). You can then later check this object via hasRunStarted.