home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 6.6 KB | 343 lines |
- import java.awt.*;
- import java.applet.Applet;
-
- /*
- * a class for handling first in, first out data structure
- */
- class FIFO {
-
- /*
- * the maximum depth of the FIFO
- */
- final int MaxDepth = 200;
-
- /*
- * the real depth of this FIFO
- */
- int depth;
-
- /*
- * write and read indexes into the data array
- */
- int writeIndex;
- int readIndex;
-
- /*
- * the number of data items currently in the FIFO
- */
- int nItems;
-
- /*
- * the data proper
- */
- int data[] = new int[MaxDepth];
-
- /*
- * width and height of the FIFO graphical display
- */
- int width;
- int height;
-
- /*
- * x and y position of the upper-left corner of the FIFO
- * graphical display
- */
- int xpos;
- int ypos;
-
- /**
- * the constructor
- * @param d - depth of the FIFO
- */
- public FIFO (int d) {
-
- depth = d;
- writeIndex = 0;
- readIndex = 0;
- nItems = 0;
-
- width = depth + 4;
- height = 50;
- xpos = 50;
- ypos = 75;
- }
-
- /**
- * write one integer value into the FIFO
- * @param value - the value to write
- */
- void write (int value) {
-
- if (nItems >= depth) return;
-
- data[writeIndex] = value;
- writeIndex += 1;
- writeIndex %= depth;
- nItems += 1;
- }
-
- /**
- * read one integer value from the FIFO
- */
- int read () {
-
- if (nItems < 1) return 0;
-
- int value = data[readIndex];
- readIndex += 1;
- readIndex %= depth;
- nItems -= 1;
-
- return value;
- }
-
- /**
- * returns true if the FIFO is empty
- */
- boolean empty () {
-
- return nItems > 0 ? false : true;
- }
-
- /**
- * returns true if the FIFO is half full
- */
- boolean halfFull () {
-
- return nItems > (depth >> 1) ? true : false;
- }
-
- /**
- * returns true if the FIFO is full
- */
- boolean full () {
-
- return nItems >= (depth) ? true : false;
- }
-
- /**
- * draws the FIFO graphical display
- * @param g - destination graphics context
- */
- void paint (Graphics g) {
-
- int x, y, w, h;
-
- g.setColor (Color.white);
- g.fillRect (xpos, ypos, width, height);
- g.setColor (Color.black);
- g.drawRect (xpos, ypos, width, height);
-
- x = writeIndex + xpos + 2;
- y = ypos - 22;
- g.drawLine (x, y, x, y + 20);
- g.drawString ("Write index "+writeIndex, x+2, y+10);
-
- x = readIndex + xpos + 2;
- y = ypos + height + 22;
- g.drawLine (x, y-20, x, y);
- g.drawString ("Read index "+readIndex, x+2, y);
-
- if (nItems < 1) return;
-
- if (nItems > (depth>>1)) g.setColor (Color.red);
- else g.setColor (Color.green);
-
- x = xpos + 2 + readIndex;
- y = ypos + 2;
- if (writeIndex > readIndex) w = nItems;
- else w = width - readIndex - 4;
- h = height - 4;
- g.fillRect (x, y, w, h);
-
- if (writeIndex > readIndex) return;
-
- x = xpos + 2;
- w = writeIndex;
- g.fillRect (x, y, w, h);
- }
- }
-
- /*
- * a class that generates data continuously
- */
- class Source extends Thread {
-
- /*
- * the FIFO to write into
- */
- FIFO fifo;
- int value;
-
- /**
- * constructor
- * saves the FIFO instance and starts the thread
- * @param f - an instance of FIFO
- */
- public Source (FIFO f) {
-
- fifo = f;
- value = 0;
-
- start ();
- }
-
- /*
- * the thread that writes one word every 100ms
- */
- public void run () {
-
- while (true) {
- synchronized (fifo) {
- if (fifo.full() == false)
- fifo.write (value++);
- }
- try {
- Thread.sleep (100);
- } catch (InterruptedException e) {
- }
- }
- }
- }
-
- /*
- * a class that reads data from the FIFO
- */
- class Sink extends Thread {
-
- /*
- * the FIFO to read from
- */
- FIFO fifo;
- int value;
-
- /**
- * constructor
- * saves the FIFO instance and starts the thread
- * @param f - an instance of FIFO
- */
- public Sink (FIFO f) {
-
- fifo = f;
-
- start ();
- }
-
- /*
- * the thread that reads all data out after the FIFO is half
- * full
- */
- public void run () {
-
- boolean empty;
- boolean halfFull;
-
- while (true) {
- synchronized (fifo) {
- halfFull = fifo.halfFull ();
- }
- if (halfFull) {
- try {
- Thread.sleep (1000);
- } catch (InterruptedException e) {
- }
- do {
- synchronized (fifo) {
- value = fifo.read ();
- }
- try {
- Thread.sleep (50);
- } catch (InterruptedException e) {
- }
- synchronized (fifo) {
- empty = fifo.empty ();
- }
- } while (empty == false);
- }
-
- try {
- Thread.sleep (100);
- } catch (InterruptedException e) {
- }
- }
- }
- }
-
- /*
- * the applet/application class
- */
- public class SyncCode extends Applet implements Runnable {
-
- Source source;
- Sink sink;
- FIFO fifo;
- Thread thread;
-
- /*
- * called when the applet is loaded
- * create instances of FIFO, Source, Sink, and Thread
- */
- public void init () {
-
- fifo = new FIFO (200);
- source = new Source (fifo);
- sink = new Sink (fifo);
-
- thread = new Thread (this);
- }
-
- /*
- * start the graphics update thread
- */
- public void start () {
-
- thread.start ();
- }
-
-
- /*
- * the graphics update thread
- * call repaint every 100ms
- */
- public void run () {
-
- while (true) {
- repaint ();
- try {
- Thread.sleep (100);
- } catch (InterruptedException e) {
- }
- }
- }
-
- /**
- * called from update() in response to repaint()
- * @param g - destination graphics context
- */
- public void paint (Graphics g) {
-
- synchronized (fifo) {
- fifo.paint (g);
- }
- }
-
- /**
- * main() is the application entry point
- * main() is unused when run as an applet
- * create a window frame and add the applet inside
- * @param args[] - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Synchronized code example");
-
- SyncCode syncCode = new SyncCode ();
- f.add ("Center", syncCode);
- f.setSize (400, 200);
- f.show ();
-
- syncCode.init ();
- syncCode.start ();
- }
- }
-