Cancel timer

I implemented a token system that assigns a fixed number of tokens. Each token, upon appointment, starts a timer that expires after a few minutes and clears that token for reuse. If the user checks the token before the timer expires, the timer must be canceled and reset with a different token validity period. It seems I can not cancel the timer due to the timer, this behavior is expected. The following are snippets:

/**
 * Fills one of the available slots with a new session key
 * @param sessionKey
 * @return true on slot fill success - false on fail
 */
public boolean fillSlot(String sessionKey)
{
    if(count<MAXCOUNT)
    {
        //Add key to slot
        slots.add(sessionKey);
        //Up the key count
        upCount();
        //Set up expiry timer
        Timer timer = new Timer();
        timer.schedule(new ExpiringTokentask(timer,sessionKey), EXPIRY_TIME);
        timers.put(sessionKey, timer);
        return true;
    }
    return false;
}

    /**
 * Check if a given key is stored in the slots
 * reset timer every time key is checked
 * @param sessionKey
 * @return true on key found false on not found
 */
public boolean checkSlot(String sessionKey)
{
    //TODO: More efficient key search and storage for larger user sets
    //TODO: Upgrade from memory array to h2 embedded DB
    for(int i=0;i<slots.size();i++)
    {
        if(sessionKey.equals(slots.get(i)))
        {
            //Reset timer
            Timer timer = timers.get(sessionKey);
            //Can't seem to do this
            // timer.cancel();
            timer.schedule(new ExpiringTokentask(timer,sessionKey), EXPIRY_TIME);
            //Return token validation
            return true;
        }
    }

    return false;
}


private class ExpiringTokentask extends TimerTask
{
    private Timer timer;
    private String expireToken;

    public ExpiringTokentask(Timer timer, String sessionKey)
    {
        this.timer = timer;
        this.expireToken = sessionKey;
        System.out.println(sessionKey);
    }

    public void run() {
        System.out.format("Time up!%n");
        clearSlot(expireToken);
        timer.cancel(); //Terminate the timer thread
    }
}
+3
source share
4 answers

As already mentioned, you can cancel the TimerTask that was sent to the timer instead of canceling the timer, so you no longer need to create new timers.

What are you doing:

timer.cancel();
timer.schedule(...);

IllegalStateExceptions, .

, , : timer.cancel() TimerTasks TimerTask Timer. , , , , . . , , .

java.util.Timer. , - TimerTasks ? ! , TimerTasks ? TimerTasks . ScheduledThreadPoolExecutor. , java.util.Timer Java.

+7

, , . , . , , TimerTask, TimerTask

+2

If the timer is not zero, you can call cancel()from any thread. The best way to do this is to use ScheduledExecutorServiceand receive Futurefor each task. In the future, you can cancel it or check the result.

+1
source

After the line where you are trying to cancel the timer, you must create a new one before calling the schedule method.

if(sessionKey.equals(slots.get(i)))
{
  //Reset timer
  Timer timer = timers.get(sessionKey);
  //Can't seem to do this
  timer.cancel();
  timer = new Timer();
  timer.schedule(new ExpiringTokentask(timer,sessionKey), EXPIRY_TIME);
  //Return token validation
  return true;
}
+1
source

Source: https://habr.com/ru/post/1723958/


All Articles