Skip to main content

Asynchronous and Synchronous

Posted by daniel on October 24, 2003 at 7:50 AM PDT

Much of the perceived performance problems with Swing can be addressed by working better with the Swing single threading model. Jonathan Simon writes that you need to "execute code in the appropriate thread" and take advantage of "asynchronous execution using SwingUtilities.invokeLater()."

This reminded me of a couple years back when Blake Stone was a panelist in one of my "Java on the Mac" sessions at MacWorld San Francisco. At that time everyone "knew" that Swing was slow, that Macs were slow, and that Swing on a Mac was deadly. Blake demo'ed JBuilder running on Mac OS X. Although he was primarily showing the benefits of their latest release, he took the time to make the following point. JBuilder was a very large pure Java application that was responsive and looked pretty good (as tools go).

In Jonathan Simon's feature article Rethinking Swing Threading he takes a close look at some of what he refers to as the synchronous verses asynchronous problem. He spends the first half of the article setting up the problem and showing a common solution to it. The problem with the commonly applied solution is that it involves

trying to represent a functional set of tasks while continuously changing threads. But changing threads requires an asynchronous model, since threads process Runnables asynchronously. Part of the problem is that we are trying to implement a synchronous model of a series of functions on top of an asynchronous threading model. That is the reason for all of the chaining and dependencies between Runnables; the order of execution and inner-class scooping issues.

It's interesting that Jonathan's event-driven approach requires increased cohesion. As he says in the conclusion

The event-driven approach forces you to place code where it belongs -- and only where it belongs. If you have a method that is executing a database call and updating UI components in the same method, you have too much logic in one class. The process of going through and analyzing the events of your system and creating an underlying event model forces you to put code only where it belongs. Code for expensive database calls does not belong in UI classes; nor do UI component updates belong in non-UI classes. With an event-driven architecture, the UI is responsible for UI updates and some database manager is responsible for database calls. At that point, each encapsulated class worries about its own threading, with minimal concern about how the rest of the system is functioning.

The next line grabbed my attention a bit. He asserts that "It certainly takes more effort up front to design and build an event-driven client, but over time, that up-front cost is far outweighed by the flexibility and maintainability of the resulting system." Do you think this is true? Do you need to do this work up front or is it appropriate to hold this optimization until it is needed?


In today's featured Weblogs .Jayson Falkner answers questions About the new JSP EL. In particular he says that the EL audience is JSP 2.0 and above developers "who want to more easily code and maintain a JSP, especially when working with more than one developer." He says things like the JSP EL are used outside of Java and JSP because

The JSP EL is pretty much an adoption of expressions used by other popular projects such as Ant and Velocity. The important concept is that you can split a dynamic web page in to two parts, code to generate dynamic values and a template to display those dynamic values through. The EL is the component that allows a template to take and display a dynamic value. Also, it is important that languages like the EL only allow for simple expressions, not full blown chunks of code such as scriptlets allow for. This helps ensure one is forced to keep business/database code and presentation code separate. Other non-Java technologies pioneered these concepts, and they use EL equivalents.

James Todd looks ahead at the December release of JXTA:03Q4[aka Timpani]. For fun, Felipe Leme publishes a recipe for the national drink of Brazil in A taste of Java . This is the recipe that Brazilian JUG leader Bruno Souza provided at the end of his JavaOne presentation. Warning - Kimmy-the-wonderwife and I have only had this drink when we are staying home.

Continuing with the Friday fun, I followed a trackback from my blog entry yesterday and found Matthew Lanham's posts on The Silent Penguin . His October 24 post "So where's the problem?" presented a requirements document for a problem as complex as any other that a developer faces each day: dressing yourself and three wet children after swimming.

Describe the quickest route to three fully dried and dressed kids (plus yourself) taking the following into consideration: each kid must wear their own clothes (on completion), the kids cannot be persuaded to access the lockers themselves, the temperature of the changing area is slightly above freezing (ok, that's a slight exaggeration) and you are only allowed to dress yourself if all 3 children are at least occupied.

As any who have attempted to solve this problem know, an Agile approach is best and refactoring is required.


In Also in Java Today , John Zukowski shows you how to convert between the old and new sets of Java collections. From old to new, you can treat a Vector as a List because Vector has been retrofitted to List List. Similarly, you can treat a Hashtable as a Map and, if you need to, you can convert an Array to a List using Arrays.asList(). You can also move from the newer collection classes to the old.

You can write your help files using XML defined by a subset of DocBook and then translate the resulting files to JavaHelp files that can be accessed from your application. Austin King shows you how to transform your files using XSL and Saxon and the JavaHelp utilities in Creating an Online Help System with javaHelp and DocBook.


In Projects and Communities , the Java User Groups community highlights the Denver JUG and their next meeting. The featured JavaPedia page is the Java Audio page. As always, feel free to contribute to the listing.


In today's java.net News Headlines :

Registered users can submit news items for the href="http://today.java.net/today/news/">java.net News Page using our
news submission form.
All submissions go through an editorial review by news director Steve
Mallet before being posted to the site. You can also subscribe to the href="http://today.java.net/pub/q/news_rss?x-ver=1.0">java.net News RSS
feed.


Current and upcoming Java Events:

Registered users can submit event listings for the href="http://www.java.net/events">java.net Events Page using our href="http://today.java.net/cs/user/create/e"> events submission form.
All submissions go through an editorial review before being posted to the
site.


This blog is delivered weekdays as the href="http://today.java.net/pub/q/java_today_rss?x-ver=1.0">Java Today RSS
feed. Once this page is no longer featured as the front page of href="http://today.java.net"> Java Today it will be archived at href="http://today.java.net/today/archive/index_10242003.html">
http://today.java.net/today/archive/index_10242003.html. You can
access other past issues in the href="http://today.java.net/today/archive/">java.net Archive.