buffer-java
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| buffer-java [2007/10/04 12:23] – franck | buffer-java [2007/10/04 14:03] (current) – franck | ||
|---|---|---|---|
| Line 32: | Line 32: | ||
| @param element the element to be added. | @param element the element to be added. | ||
| */ | */ | ||
| - | public void put(Object element) | + | public |
| { | { | ||
| while (this.inCount - this.outCount >= Buffer.SIZE) | while (this.inCount - this.outCount >= Buffer.SIZE) | ||
| Line 48: | Line 48: | ||
| @return an element of the buffer. | @return an element of the buffer. | ||
| */ | */ | ||
| - | public Object get() | + | public |
| { | { | ||
| while (this.incount - this.outCount <=0) | while (this.incount - this.outCount <=0) | ||
| Line 81: | Line 81: | ||
| public Consumer(Buffer buffer) | public Consumer(Buffer buffer) | ||
| { | { | ||
| + | super(); | ||
| this.buffer = buffer; | this.buffer = buffer; | ||
| } | } | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| public void run() | public void run() | ||
| Line 93: | Line 94: | ||
| for (int i = 0; i < ITERATION; i++) | for (int i = 0; i < ITERATION; i++) | ||
| { | { | ||
| - | System.out.println((Integer) buffer.get()); | + | System.out.println((Integer) |
| try | try | ||
| { | { | ||
| Line 100: | Line 101: | ||
| catch (InterruptedException e){} | catch (InterruptedException e){} | ||
| } | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The class Producer can be implemented as follows. | ||
| + | |||
| + | <code java> | ||
| + | /** | ||
| + | This class represents a producer. | ||
| + | |||
| + | @author Franck van Breugel | ||
| + | |||
| + | */ | ||
| + | public class Producer extends Thread | ||
| + | { | ||
| + | private Buffer buffer; | ||
| + | |||
| + | /** | ||
| + | Creates a producer for the given buffer. | ||
| + | |||
| + | @param buffer the buffer from which the producer produces. | ||
| + | */ | ||
| + | public Producer(Buffer buffer) | ||
| + | { | ||
| + | super(); | ||
| + | this.buffer = buffer; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | |||
| + | */ | ||
| + | public void run() | ||
| + | { | ||
| + | final int MILIS_IN_SEC = 1000; | ||
| + | final int ITERATION = 20; | ||
| + | for (int i = 0; i < ITERATION; i++) | ||
| + | { | ||
| + | this.buffer.put(new Integer(i)); | ||
| + | try | ||
| + | { | ||
| + | Thread.sleep((long) Math.random() * MILIS_IN_SEC); | ||
| + | } | ||
| + | catch (InterruptedException e){} | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Finally, we give an app that puts everything together. | ||
| + | |||
| + | <code java> | ||
| + | /** | ||
| + | This app creates a buffer, a consumer and a producer. | ||
| + | |||
| + | @author Franck van Breugel | ||
| + | */ | ||
| + | public class BufferTest | ||
| + | { | ||
| + | /** | ||
| + | Creates a buffer, consumer and producer. | ||
| + | */ | ||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | Buffer buffer = new Buffer(); | ||
| + | Producer producer = new Producer(buffer); | ||
| + | Consumer consumer = new Consumer(buffer); | ||
| + | producer.start(); | ||
| + | consumer.start(); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
buffer-java.1191500586.txt.gz · Last modified: by franck
