/*
 * Implement a Centralized queue solution for processing
 * requests with multiple threads, with one long query
 * and 4 short ones, to illustrate increased throughput
 *
 * Author:  Mike Criscolo
 * Date:    09/02/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;

        // Create the request and response queues
        QueueObject reqQ = new QueueObject("Request 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),
                reqQ, 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 jam them into the request queue
        for (i = 0; i < 5; i++) {
            // Last parm on next line puts a long request in first,
            // followed by short requests
            reqOb = new RequestObject("This is item " + (i + 1), ((i == 0) ? 10 : 2));
            reqQ.AddQueueItem(reqOb);
        }

        // 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();
    }

}