4

I am relatively new to java and was curious about how ActionListeners work. Say I have an action listener for a timer implemented as follows:

class TimerActionListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        //perform some operation
    }
}

What will happen if the timer is set to run faster than the code in my actionlistener class can execute. Does the code finish executing and ignore new requests until it is done (like an interrupt). Or does the new call to actionlistener take priority over the current instance - such that the code will never complete?

Ben
  • 1,132
  • 1
  • 15
  • 35

3 Answers3

8

The timer's timing is done in thread distinct from the event dispatch thread (or EDT) which is the thread that runs the code in the ActionListener. So even if the actionPerformed code is slow, the timer will keep on firing regardless and will queue its actionPerformed code on the event queue which will likely get backed up and the event thread will get clogged and the application will be unresponsive or poorly responsive.

A take-home point is to avoid calling any code that takes a bit of time on the event thread as it will make the GUI unresponsive. Consider using a SwingWorker for cases like this.

Edit: Please see trashgod's comment below for the win!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • if this is the case, is there any way to make it drop instead of queue the new requests to the actionlistener? – Ben Oct 18 '11 at 22:33
  • @Ben: a very good question, one I don't know the answer to. I'm guessing that yes, there is a way, but also that it would likely be done off of the EDT. – Hovercraft Full Of Eels Oct 18 '11 at 22:34
  • 7
    See [`setCoalesce()`](http://download.oracle.com/javase/7/docs/api/javax/swing/Timer.html#setCoalesce%28boolean%29). – trashgod Oct 18 '11 at 23:46
3

Based on the posts from hovercraft and trashgod, it seems that the Timer events do not queue by their default setting. (i.e. new timer events will be ignored until the timer event handler code has finished executing.)

Ben
  • 1,132
  • 1
  • 15
  • 35
  • `1)` I know that user with reputations <50 can post comment, then better would be to edit your post, `2)` please what did you talking about, no_code no_hash – mKorbel Oct 19 '11 at 20:35
  • I'm not really sure I understand what you mean, but to clarify, this post was intended to answer to my own question since it hadn't been explicitly stated (though credit does go to hovercraft/trashgod for pointing me in the right direction). – Ben Oct 19 '11 at 21:55
2

You can test it yourself implementing something as follows:

class TimerActionListener implements ActionListener {
    public static int inst = 1;
    public void actionPerformed(ActionEvent e) {
        int id = inst++;
        System.out.println("Executing instance: " + id);
        try { Thread.sleep(3000); } catch (Exception e) {} //For sleep 3 seconds
        System.out.println("Instance: " + id + "done! ");
    }
}
Ernesto Campohermoso
  • 7,213
  • 1
  • 40
  • 51
  • running your code seems to imply that the extra requests get dropped since the id's always seem to match up. however, it could also be queuing requests as Hovercraft suggested. since the code only prints text, it is difficult to tell if there is decreased performance as a result. – Ben Oct 18 '11 at 23:04