Download this source file


// PServer2 - An improved Print Server.  Not a complete application!
// This version uses a private inner class to implement the
// Runnable interface (the public run method).  Now the work done
// in the new Thread is completely private and can't be accessed
// from the outside.  The run method is exactly the same as before.
//
// Written 2/2001 by Wayne Pollock, Tampa Florida USA.  Adopted from
// "The Java Programming Language" 3rd Ed. by Ken Arnold, James
// Gosling, and David Holmes.  (C) 2000 by Sun Microsystems, Inc.
// Published by Addison-Wesley.  Pages 233-ff.

import java.util.LinkedList;

class PServer2
{
   private LinkedList requestList = new LinkedList();
   private ServerThread server = new ServerThread();

   public PServer2 ()
   {
      new Thread( server ).start();  // Create and start thread.
   }


   public void addPrintJob( PrintJob job )
   {
      requestList.add( job );
      // ...  Let the PServer thread know a print job has been queued.
   }


   private void printIt ( PrintJob job )
   {
      // ...  Do real job of printing here
   }


   // A private inner class:
   private class ServerThread implements Runnable
   {
      public void run ()
      {
         for ( ;; )
         {
            while ( ! requestList.isEmpty() )
               printIt( (PrintJob) requestList.removeFirst() );
            //  Wait for more print jobs.
         }
      }
   }  // End of ServerThread class
}


class PrintJob
{
   // ...
}




Send comments and mail to the WebMaster.