Go Back   Java Programming Forums > Java Standard Edition Programming Help > What's Wrong With My Code?


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2010, 03:05 PM
Junior Member
 

Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
bananasplitkids is on a distinguished road
Default Producer/Consumer Help

I wrote a program to set up the Producer/Consumer relationship and right now it is not synchronized and I want to implement Semaphores but I am having trouble with it. I want the producer/consumer threads to behave like this:
Java Code
Producer Thread:

void main()
{
int nextValueProduced; //variable to store value produced

while (!done)
{
nextValueProduced = generateTheValue(); //produce value
P( valueconsumed); //wait until value is consumed
sharedValue = nextValueProduced;
V( valueProduced);
}
}

Consumer Thread:

void main()
{
int nextValue; //variable to store consumed

while (!done)
{
P( valueProduced); //wait until value is produced
nextValueConsumed = sharedValue;
V( valueConsumed); //signal value has been consumed
processTheValue( nextvalueconsumed);
}
}
This is what I have now for the producer thread:
Java Code
public class Producer extends Thread{
	private Buffer sharedLocation;
	
	public Producer (Buffer shared) {
		super("Producer");
		sharedLocation = shared;
	}
	
	public void run() {
		for (int count = 1; count <= 4; count++){
			try {
				Thread.sleep((int) (Math.random() * 3001));
				value
				//check to see if the value has been consumed P( valueConsumed );
				sharedLocation.set(count);
				//let consumer thread know it can consume the value
			}
			catch (InterruptedException exception) {
				exception.printStackTrace();
			}
		}
		System.err.println(getName() + " done producing.");
	}
}
The consumer thread:
Java Code
public class Consumer extends Thread{
	private Buffer sharedLocation;
	
	public Consumer (Buffer shared) {
		super("Consumer");
		sharedLocation = shared;
	}
	public void run() {
		int sum = 0;
		for (int count=1; count<=4; count++) {
			try{
				Thread.sleep((int) (Math.random() * 3001));
				sum += sharedLocation.get();
			}
			catch (InterruptedException exception) {
				exception.printStackTrace();
			}
		}
		System.err.println(getName() + " done consuming. and sum= " + sum);
	}
}
This is what I have currently for semaphore:
Java Code
public class Semaphore{
	private int sharedValue;
	Semaphore valueProduced = new Semaphore(0);
	Semaphore valueConsumed = new Semaphore(1);
	
	void get(){
		try {
			



/*public class Semaphore {
	private int value;
		public Semaphore() {
		value = 0;
	}
	public Semaphore (int initial) {
		value = initial;
	}
	public void P() {
		wantToEnterCS();
		// Use any viable busy-waiting mutex solution
		while (value == 0) 
		{
		finishedInCS();
		wantToEnterCS();
		}
		value --;
		finishedInCS();
	}
	public void V() {
		wantToEnterCS();
		value++;
		finishedInCS();
	}
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:58 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.