2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


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.

YOUR FEEDBACK
Greg K wrote: 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 wrote: 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 wrote: 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 wrote: 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 me...
Daniel Blaukopf wrote: 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 c...
Roberto Leibman wrote: 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.
LATEST JAVA STORIES & POSTS
Unit testing is hard. There I said it. Although I have been developing software for the past 18 years I still find that putting my applications through their paces via unit testing is difficult. I have learned the lesson (I'm sure like many of you) the hard way. Unit testing is p...
Continuent has announced support and enhancements to MySQL Server 5.1.30 GA release, the 5.1 production version of the open source database. MySQL 5.1.30 is recommended for use on production systems by the MySQL build team at Sun Microsystems. Continuent Tungsten provides advance...
As a software journalist, there are times when certain vendors will shut the door on reporting opportunities that might represent too much of an "inside view" of their technology or their organization. I've been to more developer events than I can remember where I've been handed ...
Active Endpoints has announced the general availability of ActiveVOS 6.0.2, in response to ever increasing demands for improved process performance and efficiencies. ActiveVOS is an all-in-one, 100% standards-based orchestration and business process management system (BPM) that p...
Just because the web has been open so far doesn't mean that it will stay that way. Flash and Silverlight, arguably the two market-leading technology toolkits for rich media applications are not open. Make no mistake - Microsoft and Adobe aim to have their proprietary plug-ins, ak...
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it’s still done. At this year's JavaOne, one of the keynote JavaFX demos bombed because the network was slow, something that would be forgivable had the en...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
In every field of design one of the first things students do is learn from the work of others. They ...
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE