The Java programming language allows multiple threads of sequential flow of
control within a program. The MathAnimation
applet redisplays
techexplorer's document at regular intervals to animate a series of
mathematical equations. The animation loop runs as its own thread to prevent
taking over the main AWT thread that is in charge of all drawing and event handling.
This
section starts by detailing the steps involved in creating and
starting the MathAnimation thread. Next, the issues involved in
running and stopping the MathAnimation
thread are discussed. Can you guess which famous math equation is derived
through the animation? Try the MathAnimation applet!
Current Restriction: Various problems can occur when many synchronized threads are competing for limited resources. For example, the techexplorer listener interface follows the Observer/Subject design pattern. When techexplorer's (subject) state changes (via an event notification), it informs one or more listeners (observers) of the change. At this point, listeners may request that techexplorer take a particular action. Due to the bidirectional flow of method invocation, it is possible to encounter a deadlock situation. This is possible if in one thread techexplorer is notifying a listener while at the same time another listener thread is invoking a techexplorer method. The techexplorer Java API removes the potential for deadlock by issuing listener notifications in a new thread. However, breaking the lock that connects listener notifications and techexplorer action requests may result in unexpected behavior and such occurence is not dealt with in this version of the techexplorer Java API. The best choice is to prevent deadlock by imposing an ordering on the condition variables for your particular application.
The Java concurrency framework allows a task to be started in a new thread,
thus causing the task to proceed asynchronously.
The MathAnimation
class is declared to
implement the java.lang.Runnable
interface. The Runnable
interface dictates the implementation of the run()
method.
The MathAnimation
uses the
setPlugin()
,
setControl()
,
registerListener()
and
start()
method to
InstanceListener
,Here is the code that accomplishes this:
public void setPlugin( techexplorer obj ) { if ( obj instanceof techexplorer ) techexplorer = obj; registerListener(); } public void setControl( AxTchExpRaw obj ) { if ( obj instanceof AxTchExpRaw ) techexplorer = new techexplorerControl( obj ); registerListener(); } ... public void registerListener() { if ( techexplorer == null ) System.out.println("MathAnimation: registerListener: null techexplorer"); else { techexplorer.addInstanceListener( (InstanceListener)this ); techexplorer.disableEvents( AWTEvent.FOCUS_EVENT_MASK ); techexplorer.disableEvents( AWTEvent.KEY_EVENT_MASK); techexplorer.disableEvents( AWTEvent.MOUSE_EVENT_MASK ); techexplorer.disableEvents( AWTEvent.MOUSE_MOTION_EVENT_MASK ); } } public void start() { if ( animatorThread == null ) { animatorThread = new Thread(this); nCount = 0; } animatorThread.start(); }
For information about obtaining cross web browser handles to the ibm.techexplorer.techexplorer instance revisit our discussion, of the Java Editor applet.
The run()
method is the principal control method for Java classes that
implements the Runnable
interface.
The MathAnimation
applet's
run
method loops until either
stop()
method is invoked,
During each iteration of the loop,
the MathAnimation
applet repaints its display if the animatorThread
is still active. The paint method refreshed the techexplorer document with a
new animation "frame".
public void run() { // Lower this thread's priority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // Remember the starting time. long startTime = System.currentTimeMillis(); // Remember which thread we are. Thread currentThread = Thread.currentThread(); // This is the animation loop. while (currentThread == animatorThread) { //Display it. repaint(); //Delay depending on how far we are behind. try { startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } } }
The MathAnimation
overrides the stop()
and destroy()
method to
stop the math animation by setting the animatorThread
to null
and the bAnimating
flag to false
. Furthermore, the applet
removes itself from the list of techexplorer instance listeners.
public void stop() { //Stop the animating thread. animatorThread = null; bAnimating = false; } public void destroy() { techexplorer.removeInstanceListener( (InstanceListener)this ); }
Click on the "Start Animation" and "Stop Animation" buttons to control the
MathAnimatoin
applet. Can you guess which famous math equation is derived
through the animation?
Click here for the whole program.
Note that since some browsers do not support Java 1.1, the MathAnimation
applet is Java 1.0 compliant.
IBM techexplorer Hypermedia Browser is a trademark of the IBM Corporation. Send comments and questions to techexpl@us.ibm.com.