Home Segments Top Top Previous Next

890: Mainline

You could write the call to decrementPosition straightforwardly, as in the following illustration. Such a call is exceedingly dangerous, and likely to fail in a large application, because decrementPosition may be called while your program's main thread is in the process of executing display operations. Thus, the display operations initiated by your program's main thread and the display operations initiated by the MarqueeThread may step on each other. In such situations, your display goes haywire.

public class MarqueeThread extends Thread {
 private Marquee marquee;
 public MarqueeThread (Marquee c) {
   marquee = c;
 }
 public void run () {
  while (true) {
   // Bad programming practice, do not do this!   
   marquee.decrementPosition();                   
   try{sleep(200);}                              
   catch (InterruptedException e) {}      
  } 
 } 
}