FileDocCategorySizeDatePackage
Getter.javaAPI DocExample2393Tue Nov 13 16:41:22 GMT 2001None

Getter

public class Getter extends JPanel implements Runnable
A threaded object that removes integers from a Store object. After successfully retrieving an object, the thread sleeps for a variable time in the range 1-10 seconds

Fields Summary
private Store
theStore
private JButton
runToggle
private Log
log
private Thread
getRunner
private boolean
running
private Random
randomGenerator
Constructors Summary
public Getter(Store theStoreToUse)
Constructor

      theStore = theStoreToUse;

      setLayout( new BorderLayout() );
      log = new Log( 30, 20 );
      add( log, BorderLayout.CENTER );
      add( runToggle, BorderLayout.SOUTH );

      runToggle.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e )
         {
            if( running )
            {
               running = false;
               runToggle.setText( "Run" );
            }
            else
            {
               running = true;
               runToggle.setText( "Stop" ); 
               getRunner.interrupt();
            }
         }
      });


      getRunner = new Thread( this, "Getter" );
      running = false;
      getRunner.start();
   
Methods Summary
public voidrun()
The main method of the thread. Implements the "run-control" and the sleep period


                        
     
   
      while( true )
      {
         if( !running )
         {
            synchronized( this )
            {
               while( !running )
               {
                  try
                  {
                     wait();
                  }
                  catch( InterruptedException ie )
                  {
                  }
               }
            }
         }
         log.append( "Get a value from the store: " );
         int value = theStore.get();
         log.append( "" + value + "\n" );
         int pause = 1 + randomGenerator.nextInt( 9 );
         log.append( "Sleep for " + pause + " seconds\n" );
         try
         {
            getRunner.sleep( 1000*pause );
         }
         catch( InterruptedException ie2 )
         {}

      }