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

后端 未结 3 637
無奈伤痛
無奈伤痛 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:35

    you could also declare a boolean state called like "timerstate" or whatever and make it by default to be false. whenever you start a timer you could change this boolean to true and you'd be able to keep track of the timer.

    public boolean timerstate;
    public Timer t1;
    
    
    // some code goes here to do whatever you want
    
    
    if(timerstate == true) {
                t1.cancel();
                t1.purge();
                t1 = new Timer();   
            } else{
                t1.schedule(new TimerTask() {
    
            @Override
            public void run() {
                timerstate = true;
                //rest of code for the timer goes here
                              }
                          }
              }
    

提交回复
热议问题