Results 1 to 2 of 2

Thread: Producer-Consumer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    75

    Producer-Consumer

    hello ,
    i have tried to write a program on Producer-Consumer. !!
    But this program does not runs correctly.
    Can anybody tell whass wrong in this program ??
    thanx in advance
    Prasad
    ****************************************************
    / producer -Consumer problem

    class Storage {
    private int contents;
    private boolean available =false;



    synchronized public int get() {
    while (available = false) {
    try {
    wait();
    } catch (Exception e) {
    }
    }
    available = false;
    notify();
    return contents ;

    }


    synchronized public void put(int value) {
    while (available = true) {
    try {
    wait();
    } catch (Exception e) {
    }
    }

    available = true;
    contents = value;
    System.out.println("Value is : " + contents);
    notifyAll();
    }
    } //end of class Storage();


    class Producer extends Thread {
    Storage S1= new Storage();
    Producer(Storage s1) {

    S1=s1;

    }
    public void run() {
    for(int i=0;i<=10;i++)
    {
    System.out.println("Putting : " + i );
    S1.put(i);

    }
    } //end of run();

    } //end of class Producer

    class Consumer extends Thread {
    Storage S1= new Storage();
    Consumer(Storage s1) {
    S1=s1;
    }
    public void run() {
    try { sleep(1000);
    } catch(Exception e) {
    }
    for( int i=0;i<10;i++) {
    System.out.println("Getting : " + S1.get());

    }
    } //end of run()
    } //end of class Consumer
    ****************************************************

  2. #2
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Re: Producer-Consumer

    Will try to help...

    Originally posted by prasadkowli80

    //This while statement in the get() method is wrong
    while (available = false)

    It should be:
    Code:
    while ( available == false )

    //Same problem here in the put() method
    while (available = true)


    //This run method in the Consumer class is wrongly structured. The way you have it
    //here, it's just going to pause for a second and then print out
    //S1.get () 10 times

    public void run() {
    try { sleep(1000);
    } catch(Exception e) {
    }
    for( int i=0;i<10;i++) {
    System.out.println("Getting : " + S1.get());
    ****************************************************
    Try this instead:

    Code:
    public void run () {
    
    		for ( int i = 0; i < 10; i ++ ) {
    		
    			try { 
    				
    				System.out.println ( "Getting : " + S1.get () );
    				sleep ( 1000 ); 
    			
    			} catch ( Exception e ) {}
    
    		}
    			
    	} //end of run()
    Your main method should look like this:

    Code:
    public class Urg {
    	
    	public static void main ( String args [] ) {
    	
    		Storage S = new Storage ();		
    		Consumer C = new Consumer ( S );
    		Producer P = new Producer ( S );
    	
    		C.start ();
    		P.start ();
    	
    	}
    	
    }
    Hope that helps
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width