home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / dataloader.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.5 KB  |  101 lines

  1. // DataLoader.java
  2. // 28.02.96
  3. //
  4. // This is the class that handles all the loading of data
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.applet.Applet;
  9. import java.util.Vector;
  10. import java.util.Enumeration;
  11.  
  12.  
  13. /**
  14.  * Used to start threads to load the various databases.
  15.  */
  16. public class DataLoader {
  17.   
  18.   private Applet app;
  19.   private Appletlike applike;
  20.   private Vector threadList;
  21.   private boolean startThreads;
  22.  
  23.   public DataLoader (Applet app, Appletlike applike) {
  24.     this.app = app;
  25.     this.applike = applike;
  26.     threadList = new Vector ();
  27.   }
  28.  
  29.   /**
  30.    * Start all the threads.
  31.    */
  32.   public synchronized void start () {
  33.     DataLoaderThread aThread;
  34.  
  35.     for (Enumeration e = threadList.elements (); e.hasMoreElements ();) {
  36.       aThread = (DataLoaderThread)e.nextElement ();
  37.       if (aThread != null && !aThread.isAlive ()) {
  38.     aThread.start ();
  39.       }
  40.     }
  41.     startThreads = true;
  42.   }
  43.  
  44.   /**
  45.    * Stop all the threads.
  46.    */
  47.   public synchronized void stop () {
  48.     DataLoaderThread aThread;
  49.  
  50.     startThreads = false;
  51.     for (Enumeration e = threadList.elements (); e.hasMoreElements ();) {
  52.       aThread = (DataLoaderThread)e.nextElement ();
  53.       if (aThread != null && aThread.isAlive ())
  54.     aThread.stop ();
  55.       aThread = null;
  56.     }
  57.   }
  58.       
  59.   /**
  60.    * Create a thread to get some data.  When the thread is done, it'll
  61.    * remove itself from the thread list and update the requester.
  62.    */
  63.   public synchronized void getData (String filename, Literate reader,
  64.                     Appletlike requester) {
  65.     DataLoaderThread newLoader =
  66.       new DataLoaderThread (this, requester.getId () + threadList.size (),
  67.                 filename, reader, requester, app);
  68.     threadList.addElement (newLoader);
  69.     if (startThreads) {
  70.       newLoader.start ();
  71.     }
  72.   }
  73.  
  74.   /**
  75.    * Same as above, but reads a Vector of Strings.
  76.    */
  77.   public synchronized void getData (String filename, Appletlike requester) {
  78.     DataLoaderThread newLoader =
  79.       new DataLoaderThread (this, requester.getId () + threadList.size (),
  80.                 filename, requester, app);
  81.     threadList.addElement (newLoader);
  82.     if (startThreads) {
  83.       newLoader.start ();
  84.     }
  85.   }
  86.  
  87.   /**
  88.    * To be called by a thread when it's finished, to remove itself
  89.    * from the thread list.
  90.    */
  91.   void removeThread (DataLoaderThread doneThread) {
  92.     threadList.removeElement (doneThread);
  93.     if (threadList.size () == 0)
  94.       applike.initMessage ("Finished reading data.");
  95.   }
  96.  
  97.   public boolean finished () {
  98.     return (threadList.size () == 0);
  99.   }
  100. }    
  101.