• Post Reply Bookmark Topic Watch Topic
  • New Topic

About the problem of producer and consumer

 
lianchi gao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:class Producer implements Runnable{
SyncStack stack;
public Producer(SyncStack s){
stack = s;
}
public void run(){
for(int i=0; i<20; i++){
char c =(char)(Math.random()*26+'A');
stack.push(c);
System.out.println("生产:"+c);
try{ Thread.sleep((int)(Math.random()*100));
}catch(InterruptedException e){}
}
}
}

class Consumer implements Runnable{
SyncStack stack;
public Consumer(SyncStack s){
stack = s;
}
public void run(){
for(int i=0;i<20;i++){
char c = stack.pop();
System.out.println("消费: "+c);
try{ Thread.sleep((int)(Math.random()*1000));
}catch(InterruptedException e){}
}
}
}


class SyncStack{
private int index = 0;
private char []data = new char[6];
public synchronized void push(char c){
while(index == data.length){
try{
this.wait();
}catch(InterruptedException e){}
}
this.notify();
data[index] = c;
index++;
}
public synchronized char pop(){
while(index ==0){
try{
this.wait();
}catch(InterruptedException e){}
}
this.notify();
index--;
return data[index];
}
}

public class SyncTest{
public static void main(String args[]){
SyncStack stack = new SyncStack();
Runnable p=new Producer(stack);
Runnable c = new Consumer(stack);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}


i want to know If the implementation of this sentence
"while(index == data.length){try{
this.wait();
}catch(InterruptedException e){}
}"

do the sentence "this.notify(); data[index] = c; index++;" execute???

3q
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lianchi gao,

First things first, while posting code please put it within the code tags. Various tags can be inserted in the messages using the buttons available at the bottom of the editor.

So, you have written this code yourself of you are trying to check out some one else's code?
Anyways, the code has the following problems:

  • You must use notifyAll and not notify
  • You must call notify only after putting/removing the char in the data array
  • No point in sleeping in producers and consumers since the push and pop are blocked till they do their work
  • Condition index == data.length is redundant as it is always true
  • Push and pop must not be in a while loop but just guarded by an if condition


  • This code may look like the following:

    DISCALIMER: I have just changed the code you have posted to meet the requirements and this in no way is the perfect producer-consumer example.


    Answering your question, yeah that piece of code gets executed in two cases:
  • When the condition guarding the wait is not satisified
  • When the producer/consumer is waiting and it gets a notification call and the while loop condition is false


  • [ August 21, 2007: Message edited by: Nitesh Kant ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!