Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.
The Molecule Viewer is a fairly simple applet that allows you to view a molecular model from various angles, using the mouse to control the point of view. This demo is found in demo/MoleculeViewer/ below your JDK 1.1 directory.
The first step is to run the demo using Assure's Java runtime, instead of the regular Sun JDK (or your browser's Java, or whatever other Java engine you would normally use):
cd \java\jdk-1.1\demo\MoleculeViewer d:\java\assurej11\bin\appletviewer -J-assurej example1.html
The model pops up. Drag it around a little using the mouse, then hit Exit. Now run Assure on the log file that has been created. Because I didn't specify a name, the file is called assurej.kgi.