home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 2.5 KB | 101 lines |
- // DataLoader.java
- // 28.02.96
- //
- // This is the class that handles all the loading of data
-
- package cybcerone.utils;
-
- import java.applet.Applet;
- import java.util.Vector;
- import java.util.Enumeration;
-
-
- /**
- * Used to start threads to load the various databases.
- */
- public class DataLoader {
-
- private Applet app;
- private Appletlike applike;
- private Vector threadList;
- private boolean startThreads;
-
- public DataLoader (Applet app, Appletlike applike) {
- this.app = app;
- this.applike = applike;
- threadList = new Vector ();
- }
-
- /**
- * Start all the threads.
- */
- public synchronized void start () {
- DataLoaderThread aThread;
-
- for (Enumeration e = threadList.elements (); e.hasMoreElements ();) {
- aThread = (DataLoaderThread)e.nextElement ();
- if (aThread != null && !aThread.isAlive ()) {
- aThread.start ();
- }
- }
- startThreads = true;
- }
-
- /**
- * Stop all the threads.
- */
- public synchronized void stop () {
- DataLoaderThread aThread;
-
- startThreads = false;
- for (Enumeration e = threadList.elements (); e.hasMoreElements ();) {
- aThread = (DataLoaderThread)e.nextElement ();
- if (aThread != null && aThread.isAlive ())
- aThread.stop ();
- aThread = null;
- }
- }
-
- /**
- * Create a thread to get some data. When the thread is done, it'll
- * remove itself from the thread list and update the requester.
- */
- public synchronized void getData (String filename, Literate reader,
- Appletlike requester) {
- DataLoaderThread newLoader =
- new DataLoaderThread (this, requester.getId () + threadList.size (),
- filename, reader, requester, app);
- threadList.addElement (newLoader);
- if (startThreads) {
- newLoader.start ();
- }
- }
-
- /**
- * Same as above, but reads a Vector of Strings.
- */
- public synchronized void getData (String filename, Appletlike requester) {
- DataLoaderThread newLoader =
- new DataLoaderThread (this, requester.getId () + threadList.size (),
- filename, requester, app);
- threadList.addElement (newLoader);
- if (startThreads) {
- newLoader.start ();
- }
- }
-
- /**
- * To be called by a thread when it's finished, to remove itself
- * from the thread list.
- */
- void removeThread (DataLoaderThread doneThread) {
- threadList.removeElement (doneThread);
- if (threadList.size () == 0)
- applike.initMessage ("Finished reading data.");
- }
-
- public boolean finished () {
- return (threadList.size () == 0);
- }
- }
-