Welcome!

Java Authors: Dana Gardner, Miko Matsumura, John Ryan, Loraine Antrim, Walter H. Pinson, III

Related Topics: Java

Java: Article

Skilled Listening in Java

How to generate events for listeners correctly and efficiently

There are many good resources on using events and implementing event listeners. Unfortunately, there are nearly as many resources that incorrectly or inefficiently implement event generators. These implementations introduce subtle nondeterministic errors, execute slowly, and/or fill the virtual machine with unnecessary garbage. The goal of this article is to address this problem once and for all - as a pattern and code generator for JDKs before 1.5, and as a set of generic types in JDK 1.5 and later.

Most Java programmers learn how to listen early on, usually thanks to the Swing classes. The elements are as follows:

  • An event, which is the information to exchange.
  • A listener, which receives the information.
  • A generator, which sends the information. Listeners can be added (or removed) from generators as they become interested (or disinterested) in the events they send.
Abstractly, at any given time, the set of generators and listeners for an event form a directed graph, where edges go from generators to listeners. Usually, this graph is essentially static. That is, listeners are added to generators as part of some initialization phase, and events don't start moving through the graph until after all the connections have been made. Almost all of the implementations you see of generators work correctly, although not necessarily efficiently, in this situation.

Here's the rub. These things are often not correctly accounted for:

1.  Adding and removing listeners may happen asynchronously. That is, different threads may add or remove listeners at somewhat inconvenient times.
2.  In response to listening to an event (and so synchronously), a listener may add or remove itself or other listeners from a generator.
3.  Generators may choose to send events asynchronously.

These are all issues of correctness. Issues of efficiency are:

1.  Most generators spend most of their life sending events (as opposed to adjusting who is listening).
2.  The average number of listeners for each generator is significantly less than one.

The humble truth is, most of the generators in your application are not being listened to by anything, and most of the rest have one listener. For example, of the 18 events generated by a JButton, how many do you actually pay attention to?

Correct
Correct is more important than efficient. Listing 1 shows a typical implementation of a correct, but inefficient, generator.

Let's see why this is correct. First, the synchronized(lock) blocks synchronized access to the listeners' list. It is very important that only the clone step of send is synchronized. This is because completing a send step may require arbitrary adds or removes. If these adds or removes happen from a different thread compared to the send, they would deadlock if send(Event) is synchronized. To avoid this problem, we copy the listeners list before notifying them, thus eliminating the resource contention. It is also important that we do not use the synchronized keyword in front of the add, remove, and clone methods. This is because using synchronized in front of a method uses the object for mutually exclusive access to the methods. Since you have no control over how the DefaultGenerator will be used, it's pretty easy to set up a deadlock situation by having this public mutex locked by something else. By sticking to an internal private object for a lock, we know we're the only ones who affect it.

Safety pays in correctness (which is a good thing), but it suffers from some serious inefficiencies:

1.  The listeners list is cloned on every send. Most of the time, nobody changes the listeners during a send, so the clone does not usually matter.
2.  An Iterator is created on every send. This is another object to create and discard. Using a (pre JDK 1.5) iterator also requires runtime safety checks while going through the List.
3.  An Event is passed to every send. Most of the time, nobody is listening to the generator, so that event usually just lands in the garbage pile.

And Efficient
Sometimes correctness costs efficiency, but we are lucky here, because we can have both (see Listing 2).

To use this DefaultGenerator, we would write:

class MyGenerator
          extends DefaultGenerator {
       void send() {
             if (listening()) send(new Event());
    }
}
}


There is more code, but what have we gained? This implementation is also correct, but look at send(), listening() and send(Event): no synchronization, no copies, no type-casts, and we only create an Event if (almost always - see sidebar) we have listeners to receive it. Instead of duplicating the listener list every time a send happens, we make copies when adding and removing from the listeners array. This works because object reference assignment is an atomic action. We use a plain old array because this eliminates unnecessary runtime type casts and the iterator overhead.

We have a generator that is still safe, but faster than the naïve safe solution. But it's exactly the kind of code that is easy to get wrong. Why would we want to rewrite something a bunch of times anyway? JDK 1.5 helps us with the use of generics. Instead of using the above pattern again and again for each kind of event, we can write it once and be done. An implementation is given in the resources.

With the generics at hand, you can stop worrying about making correct and fast generators and just use them. For example, if you wanted to a DateGenerator that sends the current Date on every tick(), a listener that printed out the date, and some main code to put them together:

class DateGenerator
extends DefaultGenerator < Date > {
    public void tick() {
          if (listening()) send(new Date());
    }
}

class DateListener implements Listener < Date > {
    public void receive(Generator < Date > generator , Date date) {
       System.out.println(date);
    }
}

public class Main {
    public static void main(String[] args)
    {
          DateGenerator generator = new DateGenerator();
       DateListener listener = new DateListener();

       generator.addListener(listener);
       generator.tick();
    }
}


About Warren MacEvoy

Warren D. MacEvoy is Asst Professor of Comp Science in the department of Computer Science, Mathematics & Statistics at Mesa State College, Grand Junction, Colorado.

Comments (6) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Greg K 01/14/08 06:04:16 PM EST

I have to agree with Daniel. Perhaps a decent approach on catching exceptions during the send() might be:

protected final void send(Event event) {
final GenericListener[] tmp = listeners;

for (int i = tmp.length - 1; i >= 0; --i) {
try {
tmp[i].receive(this, event);
} catch (RuntimeException e) {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();

if (ueh != null)
ueh.uncaughtException(Thread.currentThread(), e);
log.log(Level.SEVERE, e.getMessage(), e);
}
}
}

Daniel Blaukopf 09/06/07 10:55:44 AM EDT

Hi Warren,

Looking at it again, the behavior of your implementation matches the AWT's understanding of what it means to add or remove a Listener that overrides equals(). AWT classes use == comparison instead of the equals() method in this case. I had incorrectly assumed that the semantics of add(..) and remove(..) for listeners would be the same as for collections, in which equals(..) has to be used for correct comparison.

So your implementation has the same interpretation of Listener equality as the JDK and I retract my objection.

BTW, I'm not sure if I was clear, but my main point about exceptions is that a runtime exception in a listener should not kill the event delivery thread.

Thanks,
Daniel

Warren MacEvoy 08/25/07 12:06:23 PM EDT

Additional response to Daniel B.

It was late and I was hasty writing my last response, and so apologize for the poor writing. I managed to skip your point 3 completely, so:

3. I am confused about your statement. The Generators only use the == operator on Listeners. This is because redefining equals() for some class that happened to implement Listener is probably more of coincidence than something the designer had planned to take advantage of. Am I missing something here?

Warren MacEvoy 08/22/07 09:57:42 PM EDT

Response to Daniel B.

Thanks for liking the article!

1. Since addListener and removeListener is very rarely called) efficiency isn't really an issue here. Still, there is no reason to waste time, and so the version written are efficient. arraycopy would be equivalent (but arguably slower as you pointed out) in one case, but it is only appropriate for one of the loops (in addListener). The removeListner case is more subtle because it has to skip an element during the copy.

2. I agree about the exception propagation. I see no way to handle this from a general framework, so the specific listeners need to handle this. Thanks for this point.

Response to Roberto L.

ArrayList is great in general, but bad (less efficeint) in this case. Since we are trying to solve the problem for everyone we need to just have the efficient solution in this case. Think about the System.arraycopy mentioned by Daniel B.: you don't care that it is a nasty system specific implementation instead of a pretty for loop, but instead that it is correct, efficient, and easy to use. (Which incidentally is less efficient than the for loop for this very special case). The goal here is to provide a system tool analogous to arraycopy, but for Generators, so correctness, efficiency and usability. Not convinced? Look over the swing implementation for event generators --- it's pretty ugly stuff (and I think a version of the swing classes based on the proposed framework would be faster).

So far as the generics, I have to agree that type erasure is a huge pain in the neck. If you automatically generate code using the meta classes, they generate javadocs which point out the pitfalls and how to work around them. As far as solving the problem without type erasure, you can use the pre 1.5 meta tools in a 1.5 setting.

...And figure out who to write to to *erase* type erasure from the JDK! (My version would be to make the "erased" type and a cheap interface type for each of the specific types brought to life through a generic instantiation. This is a much better trade off between code size and usability compared to C++'s template mechanism). This discussion belongs elsewhere --- I can only work in the context of the framework provided :oP

Daniel Blaukopf 08/07/07 07:08:45 AM EDT

I have a few comments on your otherwise excellent article:

1. System.arraycopy is often more clear than a copying loop. Of course, it would be slower than the loop with the presumably small array sizes you're dealing with.

2. Correct handling of exceptions is a crucial factor in a correct event generator. Listeners often contain code that is beyond the control of the author of the listening class. The code in the listeners can throw a RuntimeException. In your examples, this exception would be passed up to the caller of the event generator and other listeners would not receive the event. This is probably not the desired behavior.

3. In the unlikely event that your generator were to encounter a listener which overrode equals(), it would not work as expected.

I would suggest

i) Catching all exceptions in listeners and logging stack traces. In the absence of a log, they can go to the console. After an exception is caught, event delivery should continue.

ii) in the add and remove methods, convert the array to an ArrayList, perform the add/remove, then convert the ArrayList back to an array. It's not efficient, but it doesn't have to be and you gain correctness and clarity.

You are, of course, spot on in observing that an array is the best way to get both thread safety and minimum heap usage.

Roberto Leibman 07/27/07 01:16:30 PM EDT

OK, so I like your pattern for the generator a lot, although I think I'd rather use an ArrayList instead of the Array... array handling can be much more error prone. But that really is a minor consideration.

The bigger problem, is in the generics. And it's not a gripe with your article but with erasure in general. The last time I coded something like this I had issues because the hierarchy I chose was such that a class could not implement two Listeners where the only difference was the parametrized type. I'm not sure I can think of an easy answer though.

Thanks again for the article.