How to use Timer isRunning?

2.2k views Asked by At

I am creating a program that is creating gates(AND, OR etc.), but when I want to create an SR latch, I need to use a timer.

So when this method(inputChanged) is called it should check if the timer is running. If it is running it should do nothing. (This is were I am stuck...) Otherwise it should check if the new value deviates from the old value(getOutputValue()).

If it does it should save the new value and send an interrupt signal after a small delay. I am using setInitialDelay() for the initial delay but I don't know how to send an interrupt signal.

I wonder what I've done wrong in this code, because it is not working too well....

public void inputChanged()
{
   timer = new javax.swing.Timer(delay, new ActionListener()
   {
       public void actionPerformed(ActionEvent event) 
        {
            // Check if timer is running.
           if(timer.isRunning())
           {
               boolean value = calculateValue(); 

               //  new Value            old Value
               if(calculateValue() != getOutputValue())
               {
                   timer.setInitialDelay(delay);
                   timer.stop();
               }
               outputChanged(value);
           }
        }   
    });
    timer.start();
}
1

There are 1 answers

0
demongolem On

The logic of the code is reverse what you have written. You say if the time is running then you want to do nothing. However the code says do nothing if the timer is not running. Just a simple !timer.isRunning() should be applied to the proper line.