9 Replies - 367 Views - Last Post: 11 May 2013 - 02:56 PM Rate Topic: -----

#1 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Producer consumer problem

Posted 10 May 2013 - 05:00 PM

Something is going wrong here..after a while my fruits is not more refill by producer..its stuck to number 5


// This is method in "Buffer "class called by consumer 

 public synchronized void consumeFruits(int amount) throws                 
                                    InterruptedException {


        System.out.print(","+amount+" fruits,");
        System.out.println(" ");
        int remain = 0;
        
        if( getFruits() - amount > 0 )
        {
            fruits = fruits - amount;
        }
        else if( getFruits() - amount < 0 )
        {
            remain = amount - getFruits();
            amount = amount - remain;
            fruits = fruits - amount;
        }

        wait();
        fruits = fruits - remain;
    }



// this is again in " buffer " class called by producer

 public synchronized void reFill() {

        fruits     = fruits     + 5;
        notify();
        System.out.println("REPLENISHMENT OF RESOURCES ");
        print();
    }



//    Consumer

@Override
    public void run() {
        
        playground.register(this); 
        
        while(true) 
        {
            int num =  random.nextInt(30);
        try 
        {        
            System.out.print("Thread "+ getName() +"requests");
            playground.consumeFruits( num );
        } 
        catch (InterruptedException ex) 
        {     
        }
    } 
    }



//   Producer

 @Override
    public void run() {
        
        try
        {
            while(true) 
            {
                playground.reFill();
                Thread.sleep(400); 
            }      
        }
        catch(InterruptedException e)
        {    
        }
    }


This post has been edited by psh: 10 May 2013 - 05:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Producer consumer problem

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Producer consumer problem

Posted 10 May 2013 - 05:08 PM

Don't ignore your Exceptions. If there is a problem, you want to know about it. Use the printStackTrace() method in your catch blocks to provide you with an error message.
Was This Post Helpful? 1
  • +
  • -

#3 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 10 May 2013 - 05:12 PM

View Postmacosxnerd101, on 11 May 2013 - 12:08 AM, said:

Don't ignore your Exceptions. If there is a problem, you want to know about it. Use the printStackTrace() method in your catch blocks to provide you with an error message.


i tried to use but i got no exception or something :whatsthat:

e.printStackTrace()


Was This Post Helpful? 0
  • +
  • -

#4 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 10 May 2013 - 05:32 PM

View Postpsh, on 11 May 2013 - 12:12 AM, said:

View Postmacosxnerd101, on 11 May 2013 - 12:08 AM, said:

Don't ignore your Exceptions. If there is a problem, you want to know about it. Use the printStackTrace() method in your catch blocks to provide you with an error message.


i tried to use but i got no exception or something :whatsthat:/>

e.printStackTrace()



Something is going terribly wrong

*Playground state
fruits: 30
* Mike registers.
Mikerequests,7 fruits,
* Peter registers.
Peter requests,20 fruits,
REPLENISHMENT OF RESOURCES
*Playground state
fruits: 8
REPLENISHMENT OF RESOURCES
*Playground state
fruits: 13
Mike requests,16 fruits,
Peter requests,21 fruits,
REPLENISHMENT OF RESOURCES
*Playground state
fruits: -125
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Producer consumer problem

Posted 10 May 2013 - 06:06 PM

First you should call getFruits() only once in the whole method
you call that method at least 3 times

Now can you explain to us what is the logic behind the 3 lines of code within your if

        else if( getFruits() - amount < 0 )
        {
            remain = amount - getFruits();
            amount = amount - remain;
            fruits = fruits - amount;
        }



don't know what getFruits() return but assuming it is 5 and amount is 7
getFruits() - amount is < 0
(don;t see why you don't just check if(amount > getFruits()) a lot much clearer vy the way

so remain = 7 - 5 = 2
amount = amount - remain so 7 - 2 = 5... would have been easier so write amount = getFruits() no ???

fruiys = fruits - amount.... depends what was in fruit !!!
Was This Post Helpful? 1
  • +
  • -

#6 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 10 May 2013 - 06:51 PM

View Postpbl, on 11 May 2013 - 01:06 AM, said:

First you should call getFruits() only once in the whole method
you call that method at least 3 times

Now can you explain to us what is the logic behind the 3 lines of code within your if

        else if( getFruits() - amount < 0 )
        {
            remain = amount - getFruits();
            amount = amount - remain;
            fruits = fruits - amount;
        }



don't know what getFruits() return but assuming it is 5 and amount is 7
getFruits() - amount is < 0
(don;t see why you don't just check if(amount > getFruits()) a lot much clearer vy the way

so remain = 7 - 5 = 2
amount = amount - remain so 7 - 2 = 5... would have been easier so write amount = getFruits() no ???

fruiys = fruits - amount.... depends what was in fruit !!!


i didnt understand :(/>/>

is this what you mean?


   int numOfFruits = getFruits();
        System.out.print(","+amount+" fruits,");
        System.out.println(" ");
        int remain = 0;
        
        if( numOfFruits - amount > 0 )
        {
            fruits = fruits - amount;
        }
        else if( amount > numOfFruits )
        {
            remain = amount - getFruits();
            amount = amount - remain;
            fruits = fruits - amount;
            wait();
            fruits = fruits - remain;
// got again - value
        }
    }
   



This post has been edited by psh: 10 May 2013 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 10 May 2013 - 07:08 PM

View Postpbl, on 11 May 2013 - 01:06 AM, said:

First you should call getFruits() only once in the whole method
you call that method at least 3 times

Now can you explain to us what is the logic behind the 3 lines of code within your if

        else if( getFruits() - amount < 0 )
        {
            remain = amount - getFruits();
            amount = amount - remain;
            fruits = fruits - amount;
        }



don't know what getFruits() return but assuming it is 5 and amount is 7
getFruits() - amount is < 0
(don;t see why you don't just check if(amount > getFruits()) a lot much clearer vy the way

so remain = 7 - 5 = 2
amount = amount - remain so 7 - 2 = 5... would have been easier so write amount = getFruits() no ???

fruiys = fruits - amount.... depends what was in fruit !!!



What i want to do is:

If amount is greater that the number of fruits
then get as much you can so that the number of fruits is > 0
and then wait..until fruits is refreshed to get the remaining.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Producer consumer problem

Posted 10 May 2013 - 08:13 PM

so, you are far from that logic

and what do you mean by

Quote

and then wait..until fruits is refreshed to get the remaining.

Was This Post Helpful? 1
  • +
  • -

#9 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 11 May 2013 - 01:20 AM

View Postpbl, on 11 May 2013 - 03:13 AM, said:

so, you are far from that logic

and what do you mean by

Quote

and then wait..until fruits is refreshed to get the remaining.



For example

Consumer : Requests 5 fruits
Fruits available : 3
Consumer : got 3 fruits and waiting for 2 more..
Producer: UPDATE FRUITS(add + 5 fruits)
Consumer : got 2 fruits
Fruits available : 3

This post has been edited by psh: 11 May 2013 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#10 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Producer consumer problem

Posted 11 May 2013 - 02:56 PM

View Postpbl, on 11 May 2013 - 03:13 AM, said:

so, you are far from that logic

and what do you mean by

Quote

and then wait..until fruits is refreshed to get the remaining.


i tried this but i always number of fruits goes under zero :eek:


 public synchronized void consumeFruits(int amount) throws InterruptedException {
        
        int numOfFruits = getFruits();
        
        if( fruits - amount  >= 0 )
        {
            fruits = fruits - amount;
        }
        else if( fruits - amount < 0 )
        {
            int remaining = amount - fruits ;
            amount = numOfFruits;
            fruits = fruits - amount;

            wait();

            System.out.println(" ");
            System.out.println("Remaing consumed: "+remaining);
            fruits = fruits - remaining;
        }     
    }
        




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1