Monday, November 26, 2007

When Display Decides to Wait...

Since its been a while since I've posted anything, I thought I would write about a weird problem I was having with one of my plugins. It initially presented as a failure to load certain files in my editor. The app simply seemed to hang forever. It only showed up when running a customer build, so I first needed to connect remotely. I quickly found out that it was a problem in my logger. Since there are many complicated plugins involved with the app, I was a little relieved to find this out since it is the smallest and easiest to follow plugin. My logger is setup to use log4j to direct log messages to the Eclipse console. The files that weren't loading were logging an enormous amount of data at load time. I was using MessageConsoleStream.println to log messages to the console. This filled a buffer in IOConsolePartitioner, which eventually called ArrayList.wait(). Since this was all happening in the display thread, Eclipse never finished loading.
The fix was pretty simple and only involved a few lines of code. I realized that I would need a reusable set of threads to do the work, so I first created a thread pool using the cool new Executors class introduced in Java 1.5 (it was really nice to have this handy and not have to implement it, again). I chose to create a single thread executor so my messages would be guaranteed in order (see Executors.newSingleThreadExecutor). Then I only needed to schedule the log messages by putting them in a runnable and passing it to the executor using the execute method. Besides fixing the hang problem, the performance of my app was increased since much of the logging work didn't need to be done in Display up until the string was shown on the console.

Read More...