Results 1 to 1 of 1
Thread: Bounded Buffer
- 04-08-2010, 05:24 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 1
- Rep Power
- 0
Bounded Buffer
Tryin to complete a bounded buffer with a comsumer & producer. Have spent hours on this and cant get my head around it......... Can anybody shed some light on the code i have so far???
/*The initial problems that occur in this program seems to be synchronization.*/
// The buffer
class BoundedBuffer {
// Variables
public int dataAvailable, nextIn, nextOut;
public final int size = 100;
// Constructor
public BoundedBuffer () {
dataAvailable = size;
nextIn = nextOut = 0;
}
}
public class Producer
extends Thread {
private BoundedBuffer boundedbuffer;
public nextIn(BoundedBuffer c) {
boundedbuffer = c;
}
public void run() {
try {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
sleep((int)(Math.random() * 100));
}
} catch (InterruptedException e) { }
finally {
System.out.println("Goodbye from Producer ("
+ Thread.currentThread().getName() + ")");
}
}
}
* From Lesson: Concurrency (The Java Tutorials > Essential Classes)
*
* This is the Consumer thread, it attempts to take data out of the CubbyHole.
* The private number field below we use as a simple thread ID.
public class Consumer extends Thread {
private CubbyHole cubbyhole;
public Consumer(CubbyHole c) {
cubbyhole = c;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
}
System.out.println("Goodbye from Consumer ("
+ Thread.currentThread().getName() + ")");
}
}
// The arrivals thread
class ins extends Thread {
// Our thread works on this
public BoundedBuffer boundedbuffer;
// Constructor
public ins(BoundedBuffer boundedbuffer) {
this.boundedbuffer = boundedbuffer;
}
// What our thread does
public synchronized void run() {
while (true) {
boundedbuffer.dataAvailable--;
boundedbuffer.nextIn++;
/* Simulate a delay before the next car arrives */
try {
sleep((int)(Math.random() * 30));
} catch (InterruptedException e) { }
}
}
}
/*I think I need to include some code for the Arrivals thread not to try to move cars into a full carpark
*/
// The departures thread
class roomAvailable extends Thread {
// Our thread works on this
public BoundedBuffer boundedbuffer;
// Constructor
public roomAvailable(BoundedBuffer boundedbuffer) {
this.boundedbuffer = boundedbuffer;
}
// What our thread does
public synchronized void run() {
while (true) {
boundedbuffer.dataAvailable++;
boundedbuffer.nextOut++;
/* Simulate a delay before the next car leaves */
try {
sleep((int)(Math.random() * 30));
} catch (InterruptedException e) { }
}
}
}
/*I think I need to include some code for the Departures thread not to try to remove cars from an empty carpark.
*/
// Main
class BoundedBufferSimulator {
public static void main(String[] args) throws InterruptedException {
// Create our bag
BoundedBuffer boundedbuffer = new BoundedBuffer();
// Create our arrivals thread
ins athread = new ins(boundedbuffer);
// Create our departures thread
roomAvailable dthread = new roomAvailable(boundedbuffer);
// Start threads
athread.start();
dthread.start();
// Keep track
while (true) {
// Sleep for one seconds
Thread.sleep(1000);
// Display the current state of affairs
System.out.println("size: " + boundedbuffer.size);
System.out.println("size: " +
(boundedbuffer.size - boundedbuffer.dataAvailable));
System.out.println(" Free: " + boundedbuffer.dataAvailable);
System.out.println(" Delta: " + (boundedbuffer.size -
boundedbuffer.dataAvailable - boundedbuffer.nextIn + boundedbuffer.nextOut));
}
}
}
Similar Threads
-
Buffer Example help.
By bert682 in forum Threads and SynchronizationReplies: 0Last Post: 11-27-2009, 09:26 AM -
Need Help: Double Buffer
By rukawa527 in forum AWT / SwingReplies: 1Last Post: 02-09-2009, 12:39 AM -
Buffer
By QPRocky in forum NetworkingReplies: 2Last Post: 02-02-2009, 03:54 PM -
Bounded Array
By bugger in forum New To JavaReplies: 4Last Post: 01-04-2008, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks