Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 37.6 TRaceApplet.java—This Program Sets Up the User Interface and Starts the Race


 import java.applet.Applet;
 import java.awt.Graphics;
 import java.awt.GridLayout;

 public class TRaceApplet extends Applet implements Runnable,
 ⇒TSuspendable
 {
   private TRacer fRacers[];
   static private short fRacerCount = 0;
   private Thread fThreads[];
   static private Thread fMonitorThread;
   static private boolean fInApplet = true;
   private boolean fThreadSuspended = false;
   static private java.awt.Frame fFrame = null;
   private TWindowAdapter fWindowAdapter = null;
  
   public void init()
   {
     if (fInApplet)
     {
       String theParameter = getParameter(“NUMBER”);
       if (theParameter == null)
         fRacerCount = 0;
       else
         fRacerCount = Short.parseShort(theParameter);
     }
     if (fRacerCount <= 0)
       fRacerCount = 2;
     if (fRacerCount > Thread.MAX_PRIORITY -
 ⇒Thread.MIN_PRIORITY + 1)
       fRacerCount = (short)(Thread.MAX_PRIORITY -
 ⇒Thread.MIN_PRIORITY + 1);
      
     if (!fInApplet)
       fWindowAdapter = new TWindowAdapter();
    
     // have one column, with one row per Racer
     setLayout(new GridLayout(fRacerCount, 1));
    
     // initialize the fRacers and fThreads arrays
     fRacers = new TRacer[fRacerCount];
     fThreads = new Thread[fRacerCount];
    
     for (short i=0; i<fRacerCount; i++)
     {
       fRacers[i] = new TRacer(“Racer# “ + i, this);
      
       // scale the image so that all of the racers will fit
       fRacers[i].setSize(getSize().width,
 ⇒getSize().height/fRacerCount);
       add(fRacers[i]);
     }
   }
  
   public void start()
   {
     // set up our own “monitor” thread 

     fMonitorThread = new Thread(null, this,
 ⇒”Monitor Thread”);
     fMonitorThread.start();
   }
  
   public void stop()
   {
     fMonitorThread = null;
   }
  
   public void run()
   {
     if (fMonitorThread == Thread.currentThread())
     {
       TMouseAdapter aMouseAdapter = new TMouseAdapter();     
       for (short i=0; i<fRacerCount;i++)
       {
         // this version of the Thread constructor specifies a
 ⇒Runnable target
         fThreads[i] = new Thread(fRacers[i]);
      
         // should guarantee that the high-number thread wins
         fThreads[i].setPriority(Thread.MIN_PRIORITY+i);       
         fThreads[i].start();
         fRacers[i].addMouseListener(aMouseAdapter);     
       }
       synchronized (fMonitorThread)
       {
         fMonitorThread.notify();       
       }
     }
    
     // now the world knows that all the racers are running
     while (fMonitorThread == Thread.currentThread())
     try
     {
       fMonitorThread.sleep(100);
       synchronized(fMonitorThread)
       {
         while (fThreadSuspended)
         {
           fMonitorThread.wait();
         }
         synchronized(this)
         {
           notifyAll();
         }
       }
     } catch (InterruptedException e)
     {
       System.err.println(“The monitor thread was interrupted
 ⇒while sleeping.”);
       System.exit(1);
     }
   }
    
   public String[][] getParameterInfo()
   {
    short theMaximumNumberOfThreads =
 ⇒(short)(Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1);
    String theParameterInfo[][] =
    {
      {“NUMBER”,    “1-”+theMaximumNumberOfThreads,
 ⇒”number of racers”},
    };
    return theParameterInfo;
   }
  
   public String getAppletInfo()
   {
     String theAppletInfo = “Author: Michael L. Morgan\nDate:
 ⇒19 March 98\nInspired by the Great Thread Race
 ⇒(Special Edition Using Java, Que, 1996, p. 551)”;
     return theAppletInfo;
   }
  
   public boolean isSuspended()
   {
     return fThreadSuspended;
   }
  
   public static void main(String argv[])
   {
     fInApplet = false;
    
     //look for the number of racers on the command line
     if (argv.length>0)
     try {
       fRacerCount = Short.parseShort(argv[0]);
     } catch (NumberFormatException e)
     {
       fRacerCount = 5;
     }
      
     fFrame = new java.awt.Frame(“Racing Threads”);
    
     TRaceApplet theRace = new TRaceApplet();
     fFrame.setSize(400,200);
     fFrame.add(theRace, java.awt.BorderLayout.CENTER);
     fFrame.show();
     theRace.init();
    
     // be sure to wait until after init() to hook up listener
     fFrame.addWindowListener(theRace.fWindowAdapter);
     fFrame.pack();
     theRace.start();

     // don’t pass here till all racers are started

     synchronized (fMonitorThread)
     {       
       try {
         fMonitorThread.wait();
       } catch (InterruptedException e)
       {
         System.err.println(“Main thread interrupted while
 ⇒waiting for racers to start.”);
         System.exit(1);
       }
     }
     System.out.println(“And they’re off!”);

     // wait till all the racers are finished
     for (short i=0; i<fRacerCount; i++)
     try
     {
       theRace.fThreads[i].join();
     } catch (InterruptedException e)
     {
       System.err.println(“The monitor thread was interrupted
 ⇒while waiting for the other threads to exit.”);
     }
     System.exit(0);
   }
  
   class TWindowAdapter extends java.awt.event.WindowAdapter
   {
     public void windowClosing(java.awt.event.WindowEvent
 ⇒anEvent)
     {
       fFrame.setVisible(false);
       fFrame.dispose();
       System.exit(0);
     }
   }
   class TMouseAdapter extends java.awt.event.MouseAdapter
   {
     public synchronized void mousePressed(
 ⇒java.awt.event.MouseEvent anEvent)
     {
       anEvent.consume();
       fThreadSuspended = !fThreadSuspended;
       if (!fThreadSuspended)
         synchronized (fMonitorThread)
         {
           fMonitorThread.notifyAll();
         }
     }
   }
 }


Whenever possible, include code such as main() in your own applets. This is shown in TraceApplet beginning about 2/3 into the listing with
public static void main(String argv[])
and ending with
System.exit(0);
}
When you include code such as this, you can run your program as an applet or as an application. You’ll want to run as an application when you regression test, or if you later reuse the code outside of a browser environment.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.