Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
Therefore, I was shocked by what Assure showed me about some of my "thoroughly debugged" Java code. Assure is an easy to use runtime behavioral monitoring and analysis tool that was developed by Kuck & Associates Inc. (KAI). It found problems that were so glaringly obvious (once pointed out) that I would be embarrassed to outline them. It also found problems so subtle that I had to think a long time before I could even imagine how there could be such a problem.
This article will explore the tip of the iceberg, the very easiest part of the problem to see, by using Assure to debug example code provided by Sun with the JDK. Wouldn't you assume that such code was as close to bug-free as is humanly possible? This isn't the case, as we shall see. In the process of reading this article, perhaps you'll learn a little about writing code that is thread-safe to begin with, as I did in writing this article.
Languages and environments other than Java permit the use of multiple threads. But Java applications are virtually impossible
to write without using them, and most apps use a lot of them. Even the most trivial applet will have three threads: the applet's
main run loop, the Java virtual machine's event loop, and the Java display manager's paint loop.
To this we can probably add even more threads for applications with animation timing loops, sound, and network communication. There can be no argument that Java's runtime environment is replete with threads, all running simultaneously and manipulating the same data. To deal with this chaos, Java provides just one low-level construct: the synchronized keyword.
Synchronization is both necessary and sufficient to control the execution of multithreaded code, but that's akin to saying a raft and an oar are enough to cross any body of water. Sure, you can do it. In practice, however, synchronization is very difficult to use correctly. Here's a list of some of the problems inherent with synchronization:
if(a==1) { b=2; }
if((a==1)&&(b!=2)) { throw new Error("impossible error!"); }
Without turning this article into a tutorial on multithreaded programming, here are some good rules of thumb that are commonly used in Java programming. (For previous JavaWorld articles on multithreading, see Resources.)
paint method, or do all your drawing in your run loop, and use the paint method only to set a "needs to be painted" flag. Conversely, do not do any drawing directly in response to various mouse, button, and network events.One day I was trying to solve a particularly hard-to-find display glitch, the kind that, to my eye, simply reeked of a synchronization problem. As I frequently do when temporarily stumped, I consulted my peers on Usenet, using Deja News (see Resources), and found a brief reference to Assure. Assure is a runtime analysis tool that examines your program's behavior in two phases:
For the purposes of this article, I decided to use one of Sun's own examples as a guinea pig; both because these examples are supposed to be good examples, and because they're readily available, simple, clean little programs. I didn't have to look very hard; the Molecule Viewer demo was the second one I looked at using Assure.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq