/*
 * Implement a Distributed queue solution for processing requests
 * with multiple threads
 *
 * Author: Mike Criscolo
 * Date:   09/01/97
 */
public class PrimaryThread extends Thread {

    private int totThreads;
    
    public PrimaryThread() {

        super("PrimaryThread");
        
        // We'll do 2 threads
        totThreads = 2;
    }

    public void run() {

        int i;
        InspectorThread  threadArray[];
        RequestObject   reqOb;
        RequestObject   rspOb;
        int curThread = 0;

        // Create the response queue
        QueueObject rspQ = new QueueObject("Response Queue");
        
        threadArray = new InspectorThread[totThreads];

        // Crank the threads
        System.out.println("Cranking threads...");
        for (i = 0; i < totThreads; i++) {
            threadArray[i] = new InspectorThread("User Thread " + (i + 1), (i + 1), rspQ);
            threadArray[i].start();
        }

        // Give the threads a chance to crank
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("Bumped off the sleep() call!");
        }

        // Create some RequestObjects and hand them to the 
        // InspectorThread objects...
        for (i = 0; i < 5; i++) {
            reqOb = new RequestObject("This is item " + (i + 1), 2);
            threadArray[curThread].submitRequest(reqOb);
            curThread++;
            if (curThread == totThreads) {
                curThread = 0;
            }
        }

        // Now loop, checking the response queue for all responses
        while (i > 0) {
            rspOb = (RequestObject)rspQ.GetQueueItem();
            rspOb.Dump();
            i--;
        }

        // Kill all threads
        for (i=0; i<totThreads; i++) {
            threadArray[i].shutdown();
        }
        
    }

    public static void main(String argv[]) {
        PrimaryThread  pt;

        pt = new PrimaryThread();
        pt.start();
    }
}