Thread Watch

When off-loading a lengthy operation to a different thread, the preferred method of monitoring the thread's progress is to use a SwingWorker instance.

Yet another technique passes a continuation, as shown here. A similar approach is discussed in more detail by Goetz, et al. in chapter 9 of Java Concurrency in Practice. Correct synchronization in general is discussed in this video presentation Advanced Topics in Programming Languages: The Java Memory Model

With some care, the alternative approach below may be used. Having a simple MVC architecture, the Model evolves an offscreen image in a separate thread. The probability that a pixel will be modified in an iteration of the Model is given by a Random number having a Gaussian distribution. This produces the ripple effect seen below. A Swing Timer periodically notifies the View that new image data should be rendered. A Control instance allows the image to be reset and the update rate adjusted. The fill checkbox causes the Model to switch to a gray-scale palette. As the window is resized or the rate adjusted, it is easy to see the image being updated while it is also being scaled to fill the View.

Copyright © 2009 John B. Matthews. Distributed under the terms of the GPL