//============================================================================== // FifoQueueTest.java //============================================================================== package tribble.util; // System imports import java.lang.Exception; import java.lang.IllegalArgumentException; import java.lang.Integer; import java.lang.InterruptedException; import java.lang.NullPointerException; import java.lang.String; import java.lang.Thread; /******************************************************************************* * Generic FIFO queue test driver. * Tests the {@link FifoQueue} class. * * * @version $Revision: 1.1 $ $Date: 2007/06/21 21:26:26 $ * @since 2007-06-21 * @author David R. Tribble (david@tribble.com). *

* Copyright ©2007 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by * by the United States Department of State as a terrorist, or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. */ class FifoQueueTest extends java.lang.Thread { // Identification /** Revision information. */ static final String REV = "@(#)tribble/util/FifoQueueTest.java $Revision: 1.1 $ $Date: 2007/06/21 21:26:26 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Constants private static final String PRODUCER = "producer"; private static final String CONSUMER = "consumer"; private static final int N_ITEMS = 2000; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods /*************************************************************************** * Test driver for class {@link FifoQueue}. * *

* Usage *

* * java tribble.util.FifoQueueTest size * * * @param args * Command line arguments. * * @since 1.1, 2007-06-21 */ public static void main(String[] args) throws Exception { int size = 10; FifoQueue queue; FifoQueueTest prod; FifoQueueTest cons; // Check args if (args.length >= 1) size = Integer.parseInt(args[0]); // Spawn two threads, a consumer and a producer queue = new FifoQueue(size); cons = new FifoQueueTest(CONSUMER, queue); prod = new FifoQueueTest(PRODUCER, queue); cons.start(); prod.start(); while (!cons.m_done || !prod.m_done) pause(300); // Done System.out.println("Done"); System.exit(0); } /*************************************************************************** * Sleep for a specified amount of time. * * @param msec * Milliseconds to sleep. * * @since 1.1, 2007-06-21 */ private static void pause(int msec) { try { Thread.sleep(msec); } catch (InterruptedException ex) { } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Variables private String m_type; private FifoQueue m_queue; private volatile boolean m_done; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Constructors /*************************************************************************** * Constructor. * Creates a FIFO queue containing a specified number of items. * * @param size * Maximum number of items that the queue can hold at one time. * * @throws IllegalArgumentException (unchecked) * Thrown if size is less than 1. * * @since 1.1, 2007-06-21 */ private FifoQueueTest(String type, FifoQueue queue) { // Initialize m_type = type; m_queue = queue; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Methods /*************************************************************************** * Test driver method for class {@link FifoQueue}. * Called by {@link #main main()}. * * @param args * Command line arguments. * * @since 1.1, 2007-06-21 */ public void run() { try { if (m_type == PRODUCER) runProducer(); else runConsumer(); } catch (Exception ex) { System.out.println("*** Exception:"); ex.printStackTrace(System.out); } m_done = true; } private void runProducer() { int n = 0; int fulls = 0; // Add items to the queue while (n < N_ITEMS) { // Add ten items to the queue for (int j = 0; j < 10 && n < N_ITEMS; j++) { String item; int ix; item = "ITEM_" + (n+1); ix = m_queue.m_putI; if (m_queue.put(item)) { n++; System.out.println("> put " + n + ": " + item + " @[" + ix + "]"); } else { System.out.println("> put " + n + ": queue full"); fulls++; if (fulls > N_ITEMS) { System.out.println("ABORT producer"); return; } pause(80); } } // Sleep a spell if (n%1000 >= 300 && n%1000 < 350) pause(1000); if (n < N_ITEMS) pause(100); } System.out.println("producer done: " + n + " items"); } private void runConsumer() { int n = 0; int empties = 0; // Take items from the queue while (n < N_ITEMS) { // Take seven items from the queue for (int j = 0; j < 7 && n < N_ITEMS; j++) { String item; int ix; ix = m_queue.m_getI; item = (String) m_queue.get(); if (item != null) { n++; System.out.println("<< get " + n + ": " + item + " @[" + ix + "]"); } else { System.out.println("<< get " + n + ": queue empty"); empties++; if (empties > N_ITEMS) { System.out.println("ABORT consumer"); return; } pause(80); } } // Sleep a spell if (n%1000 >= 0 && n%1000 < 50) pause(1500); pause(150); } System.out.println("consumer done: " + n + " items"); } } // End FifoQueueTest.java