Is there a code to check if a timer is running?

后端 未结 3 638
無奈伤痛
無奈伤痛 2021-01-17 17:02

I have a game where I am scheduling a timer. I have this CoresManager file:

package com.rs.cores;

 import java.util.Timer;
 import java.util.concurrent.Exec         


        
3条回答
  •  情话喂你
    2021-01-17 17:46

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

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

提交回复
热议问题