home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch04 / WaitDemo.java < prev   
Encoding:
Java Source  |  1998-12-14  |  6.1 KB  |  330 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. /*
  5.  * a class for handling first in, first out data structure
  6.  */
  7. class FIFO {
  8.  
  9. /*
  10.  * the maximum depth of the FIFO
  11.  */
  12. final int MaxDepth = 200;
  13.  
  14. /*
  15.  * the real depth of this FIFO
  16.  */
  17. int depth;
  18.  
  19. /*
  20.  * write and read indexes into the data array
  21.  */
  22. int writeIndex;
  23. int readIndex;
  24.  
  25. /*
  26.  * the number of data items currently in the FIFO
  27.  */
  28. int nItems;
  29.  
  30. /*
  31.  * the data proper
  32.  */
  33. int data[] = new int[MaxDepth];
  34.  
  35. /*
  36.  * width and height of the FIFO graphical display
  37.  */
  38. int width;
  39. int height;
  40.  
  41. /*
  42.  * x and y position of the upper-left corner of the FIFO
  43.  * graphical display
  44.  */
  45. int xpos;
  46. int ypos;
  47.  
  48. /**
  49.  * the constructor
  50.  * @param d - depth of the FIFO
  51.  */
  52. public FIFO (int d) {
  53.  
  54.        depth = d;
  55.        writeIndex = 0;
  56.        readIndex = 0;
  57.        nItems = 0;
  58.  
  59.        width = depth + 4;
  60.        height = 50;
  61.        xpos = 50;
  62.        ypos = 75; 
  63. }
  64.  
  65. /**
  66.  * write one integer value into the FIFO
  67.  * invoke wait() if the FIFO is full
  68.  * @param value - the value to write
  69.  */
  70. synchronized void write (int value) {
  71.  
  72.        if (nItems >= depth) {
  73.               try {
  74.                      wait ();
  75.               } catch (InterruptedException e) {
  76.               }
  77.        }
  78.  
  79.        data[writeIndex] = value;
  80.        writeIndex += 1;
  81.        writeIndex %= depth;
  82.        nItems += 1;
  83.        notify ();
  84. }
  85.  
  86. /**
  87.  * read one integer value from the FIFO
  88.  * invoke wait() if the FIFO is empty
  89.  */
  90. synchronized int read () {
  91.  
  92.        if (nItems < 1) {
  93.               try {
  94.                      wait ();
  95.               } catch (InterruptedException e) {
  96.               }
  97.        }
  98.  
  99.        int value = data[readIndex];
  100.        readIndex += 1;
  101.        readIndex %= depth;
  102.        nItems -= 1;
  103.        notify ();
  104.  
  105.        return value;
  106. }
  107.  
  108. /**
  109.  * returns true if the FIFO is empty
  110.  */
  111. synchronized boolean empty () {
  112.  
  113.        return nItems > 0 ? false : true; 
  114. }
  115.  
  116. /**
  117.  * returns true if the FIFO is half full
  118.  */
  119. synchronized boolean halfFull () {
  120.  
  121.        return nItems > (depth >> 1) ? true : false;
  122. }
  123.  
  124. /**
  125.  * returns true if the FIFO is full
  126.  */
  127. synchronized boolean full () {
  128.  
  129.        return nItems >= (depth) ? true : false; 
  130. }
  131.  
  132. /**
  133.  * draws the FIFO graphical display
  134.  * @param g - destination graphics context
  135.  */
  136. synchronized void paint (Graphics g) {
  137.  
  138.        int x, y, w, h;
  139.  
  140.        g.setColor (Color.white);
  141.        g.fillRect (xpos, ypos, width, height);
  142.        g.setColor (Color.black);
  143.        g.drawRect (xpos, ypos, width, height);
  144.  
  145.        x = writeIndex + xpos + 2;
  146.        y = ypos - 22;
  147.        g.drawLine (x, y, x, y + 20);
  148.        g.drawString ("Write index "+writeIndex, x+2, y+10);
  149.  
  150.        x = readIndex + xpos + 2;
  151.        y = ypos + height + 22;
  152.        g.drawLine (x, y-20, x, y);
  153.        g.drawString ("Read index "+readIndex, x+2, y);
  154.  
  155.        if (nItems < 1) return;
  156.  
  157.        if (nItems > (depth>>1)) g.setColor (Color.red);
  158.        else g.setColor (Color.green);
  159.  
  160.        x = xpos + 2 + readIndex;
  161.        y = ypos + 2; 
  162.        if (writeIndex > readIndex) w = nItems;
  163.        else w = width - readIndex - 4;
  164.        h = height - 4;
  165.        g.fillRect (x, y, w, h);
  166.  
  167.        if (writeIndex > readIndex) return;
  168.  
  169.        x = xpos + 2;
  170.        w = writeIndex;
  171.        g.fillRect (x, y, w, h);
  172. }
  173. }
  174.  
  175. /*
  176.  * a class that generates data continuously
  177.  */
  178. class Source extends Thread {
  179.  
  180. /*
  181.  * the FIFO to write into
  182.  */
  183. FIFO fifo;
  184. int value;
  185.  
  186. /**
  187.  * constructor
  188.  * saves the FIFO instance and starts the thread
  189.  * @param f - an instance of FIFO
  190.  */
  191. public Source (FIFO f) {
  192.  
  193.        fifo = f;
  194.        value = 0;
  195.  
  196.        start ();
  197. }
  198.  
  199. /*
  200.  * the thread that writes one word every 50ms on average
  201.  */
  202. public void run () {
  203.  
  204.        while (true) {
  205.               if (fifo.full() == false)
  206.                      fifo.write (value++);
  207.  
  208.               try {
  209.                      Thread.sleep ((int) (100 * Math.random ()));
  210.               } catch (InterruptedException e) {
  211.               }
  212.        }
  213. }
  214. }
  215.  
  216. /*
  217.  * a class that reads data from the FIFO
  218.  */
  219. class Sink extends Thread {
  220.  
  221. /*
  222.  * the FIFO to read from
  223.  */
  224. FIFO fifo;
  225. int value;
  226.  
  227. /**
  228.  * constructor
  229.  * saves the FIFO instance and starts the thread
  230.  * @param f - an instance of FIFO
  231.  */
  232. public Sink (FIFO f) {
  233.  
  234.        fifo = f;
  235.  
  236.        start ();
  237. }
  238.  
  239. /*
  240.  * the thread that tries to read one word every 50ms on average
  241.  */
  242. public void run () {
  243.  
  244.        while (true) {
  245.               value = fifo.read ();
  246.  
  247.               try {
  248.                      Thread.sleep ((int) (100 * Math.random ()));
  249.               } catch (InterruptedException e) {
  250.               }
  251.        }
  252. }
  253. }
  254.  
  255. /*
  256.  * the applet/application class
  257.  */
  258. public class WaitDemo extends Applet implements Runnable {
  259.  
  260. Source source;
  261. Sink sink;
  262. FIFO fifo;
  263. Thread thread;
  264.  
  265. /*
  266.  * called when the applet is loaded
  267.  * create instances of FIFO, Source, Sink, and Thread
  268.  */
  269. public void init () {
  270.  
  271.        fifo = new FIFO (200);
  272.        source = new Source (fifo);
  273.        sink = new Sink (fifo);
  274.  
  275.        thread = new Thread (this);
  276. }
  277.  
  278. /*
  279.  * start the graphics update thread
  280.  */
  281. public void start () {
  282.  
  283.        thread.start ();
  284. }
  285.  
  286.  
  287. /*
  288.  * the graphics update thread
  289.  * call repaint every 100ms
  290.  */
  291. public void run () {
  292.  
  293.        while (true) {
  294.               repaint ();
  295.               try {
  296.                      Thread.sleep (100);
  297.               } catch (InterruptedException e) {
  298.               }
  299.        }
  300. }
  301.  
  302. /**
  303.  * called from update() in response to repaint()
  304.  * @param g - destination graphics context
  305.  */
  306. public void paint (Graphics g) {
  307.  
  308.        fifo.paint (g);
  309. }
  310.  
  311. /**
  312.  * main() is the application entry point
  313.  * main() is unused when run as an applet
  314.  * create a window frame and add the applet inside
  315.  * @param args[] - command-line arguments
  316.  */
  317. public static void main (String args[]) {
  318.  
  319.        Frame f = new Frame ("FIFO Demo");
  320.  
  321.        WaitDemo waitDemo = new WaitDemo ();
  322.        f.add ("Center", waitDemo);
  323.        f.setSize (400, 200);
  324.        f.show ();
  325.        
  326.        waitDemo.init ();
  327.        waitDemo.start ();
  328. }
  329. }
  330.