Results 1 to 8 of 8
  1. #1
    Azuki is offline Member
    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Default My simulation for concurrent processing didn't work

    I am suppose to write a program to simulate this situation.

    A tribe of savages eats communal dinners from a large pot that can hold M servings of stewed missionaries. When a savage wants to eat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage wakes up the cook and then waits until the cook has refilled the pot.

    Admin class: This class creates the cook and generates arrival of 10 savages. The class also keeps track of the number of servings in the pot (using an array of size 1) and prints the number of servings left in the pot after all the savages have eaten.

    Cook class: This class models the cooking done by the cook.
    It should have a method putServingsInPot() that performs the following:
    • generate a random number M (between 1 and 5 inclusive)
    • show a message the cook has put M missionaries into the pot.
    • include a timing delay to represent the cooking relative to M.

    This method is invoked only when the pot is empty.

    Savage class: This class models the behaviour of the savages.
    It should have a suitable method that performs the following:
    get a serving from the pot
    • include a timing delay to represent the time spent eating
    • deduct one serving from the pot
    • show messages that the savage starts eating and has finished eating

    When getting a serving from the pot, if the pot is empty, the cook’s method putServingsInPot() needs to be invoked.

    These are my codes but the output is totally wrong. But I just can't understand where goes wrong Please help!

    Java Code:
    public class Admin
    {
        public static void main (String[] args)
        {
            
            Cook cook = new Cook();
            Savage[] savage = new Savage[10];
            int[] numOfServings = new int[1];
            
            
            for (int i = 0; i<savage.length; i++)
            {
                savage[i] = new Savage(i+1, numOfServings, cook);
                savage[i].start();
       
            }
            
            
            System.out.println("ending...There are " + numOfServings[0] + " missionaries left in the pot");
        }
    }
    Java Code:
    public class Cook extends Thread
    {
    
        
        
        public synchronized void putServingsInPot(int[] numOfServings)
        {
            int m = (int)(Math.random()*5)+1;
            Timer.cook(m);
            numOfServings[0]+=m;
            System.out.println("Cook has put " + m + " missionaries into pot.");
            notifyAll();
        }
    }
    Java Code:
    public class Savage extends Thread
    {
        private int id;
        private int[] numOfServings;
        private Cook c;
        
        public Savage(int id, int[] numOfServings, Cook c)
        {
            this.id=id;
            this.numOfServings = numOfServings;
            this.c=c;
        }
        
        public void run()
        {
            for(int i=0; i<2; i++)
            {
                getServingFromPot();
            }
        }
        
        public synchronized void getServingFromPot()
        {
           try
           {
               while(numOfServings[0]==0)
               {
                   c.putServingsInPot(numOfServings);
                   wait();
               }
               System.out.println("Salvage " + id + " getting a serving to eat");
               Timer.eat(id);
               numOfServings[0]--;
               System.out.println("Salvage " + id + " finished eating");
           }
           catch (Exception e)
           {
               
           }
           
        }
    }
    Java Code:
    public class Timer
    {
        public static void eat(int time)
        {
            duration ((time+10)*2);
        }
        
        public static void cook (int share)
        {
            duration (share*100);
        }    
        
        public static void duration(int time)
        {
            int speedFactor = 1000;
            for(int j = 0; j<time; j++)
            {
                for(int k = 0; k<speedFactor; k++)
                {
                    double r = (k/2)*2;
                }
            }
        }
    }
    The output suppose to be like this:
    Cook has put 5 missionaries into pot
    Savage 0 getting a serving to eat
    Savage 3 getting a serving to eat
    Savage 5 getting a serving to eat
    Savage 1 getting a serving to eat
    Savage 9 getting a serving to eat
    There are no more missionaries in the pot!
    Cook has put 2 missionaries into pot
    Savage 5 finished eating
    Savage 3 finished eating
    Savage 0 finished eating
    Savage 1 finished eating
    Savage 9 finished eating
    Savage 7 getting a serving to eat
    Savage 8 getting a serving to eat
    There are no more missionaries in the pot!
    Cook has put 5 missionaries into pot
    Savage 8 finished eating
    Savage 7 finished eating
    Savage 6 getting a serving to eat
    Savage 4 getting a serving to eat
    Savage 2 getting a serving to eat
    Savage 4 finished eating
    Savage 6 finished eating
    Savage 2 finished eating
    There are 2 missionaries in the pot!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: My simulation for concurrent processing didn't work

    You showed what the output is supposed to be but not what you are producing. Can you include that please?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: My simulation for concurrent processing didn't work

    Well, I got it to compile and it isn't working as expected. However, I did notice that you didn't start your cook thread. In fact, you didn't override the run() method in your thread even though you subclassed Thread. Also, there was at least one try/catch that ignored the exception. At a minimum you should print the stack trace.

    Regards,
    Jim
    Last edited by jim829; 03-16-2013 at 05:55 PM. Reason: found other problems
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Azuki is offline Member
    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Default Re: My simulation for concurrent processing didn't work

    Quote Originally Posted by jim829 View Post
    You showed what the output is supposed to be but not what you are producing. Can you include that please?

    Regards,
    Jim
    My output is totally wrong:
    Cook has put 2 missionaries into pot.
    ending...There are 2 missionaries left in the pot
    Salvage 9 getting a serving to eat
    Salvage 9 finished eating
    Salvage 9 getting a serving to eat
    Salvage 9 finished eating
    Salvage 8 getting a serving to eat
    Cook has put 2 missionaries into pot.
    Salvage 8 finished eating
    Salvage 8 getting a serving to eat
    Cook has put 3 missionaries into pot.
    Salvage 8 finished eating
    Cook has put 4 missionaries into pot.
    Cook has put 5 missionaries into pot.
    Cook has put 1 missionaries into pot.
    Cook has put 5 missionaries into pot.
    Cook has put 3 missionaries into pot.

  5. #5
    Azuki is offline Member
    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Default Re: My simulation for concurrent processing didn't work

    The cook thread should start only when the pot is empty. I rewrite my classes here, but still it doesn't work.

    Java Code:
    public class Cook extends Thread
    {
    	private int[] numOfServings;
    
    	public Cook(int[] numOfServings)
    	{
    		this.numOfServings = numOfServings;
    	}
    
    	public void run()
    	{
    			putServingsInPot(numOfServings);
    	}
    
        private synchronized void putServingsInPot(int[] numOfServings)
        {
            int m = (int)(Math.random()*5)+1;
            Timer.cook(m);
            numOfServings[0]+=m;
            System.out.println("Cook has put " + m + " missionaries into pot.");
            notifyAll();
        }
    }
    Java Code:
    public class Savage extends Thread
    {
        private int id;
        private int[] numOfServings;
        private Cook c;
    
        public Savage(int id, int[] numOfServings, Cook c)
        {
            this.id=id;
            this.numOfServings = numOfServings;
            this.c=c;
        }
    
        public void run()
        {
            for(int i=0; i<2; i++)
            {
    
    		    getServingFromPot();
    
            }
        }
    
        private synchronized void getServingFromPot()
        {
    
            while(numOfServings[0]==0)
       		{
           		c.start();
    
    	           try{
    	           	wait();
    	           } catch (Exception e){}
    	    }
    
    		//out of while means cook has put missionaries into pot
           System.out.println("Salvage " + id + " getting a serving to eat");
           Timer.eat(id);
           numOfServings[0]--;
           System.out.println("Salvage " + id + " finished eating");
    
        }
    }
    Java Code:
    public class Admin
    {
        public static void main (String[] args)
        {
    		int[] numOfServings = new int[1];
            Cook cook = new Cook(numOfServings);
            Savage[] savage = new Savage[10];
    
    
    
            for (int i = 0; i<savage.length; i++)
            {
                savage[i] = new Savage(i+1, numOfServings, cook);
                savage[i].start();
    
            }
    
    
            System.out.println("ending...There are " + numOfServings[0] + " missionaries left in the pot");
        }
    }

    Quote Originally Posted by jim829 View Post
    Well, I got it to compile and it isn't working as expected. However, I did notice that you didn't start your cook thread. In fact, you didn't override the run() method in your thread even though you subclassed Thread. Also, there was at least one try/catch that ignored the exception. At a minimum you should print the stack trace.

    Regards,
    Jim

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: My simulation for concurrent processing didn't work

    The cook method should be started at the beginning. In the run method, you need a synchronized block with a wait statement. Something like this (but additional code will probably be necessary. Like letting the Savages know the pot has been updated).


    Java Code:
    synchronized(lock) {
          while (potContents > 0) {// non-empty pot
                lock.wait();
                updatePot();  // must update potContents or loop will exit
          }
    }
    If you are unfamiliar with threads, please check The Java™ Tutorial.


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Azuki is offline Member
    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Default My wait() and notify() wouldnt work.

    I modified my codes as below and start my Cook thread in Admin. But still the result is not what I expect

    Java Code:
    public class Cook extends Thread
    {
        private int[] numOfServings;
        
        public Cook (int[] numOfServings)
        {
            this.numOfServings = numOfServings;
        }
        
        public void run()
        {
            putServingsInPot(numOfServings);
        }
    
        public synchronized void putServingsInPot (int[] numOfServings)
        {
            int m = (int)(Math.random()*5)+1;
            Timer.cook(m);
            numOfServings[0]+=m;
            System.out.println("Cook has put " + m + " missionaries into pot.");
            notifyAll();
            
        }
        
    }
    Java Code:
    public class Savage extends Thread
    {
        private int[] numOfServings;
        private int id;
        private Cook c;
    
        public Savage(int[] numOfServings, int id, Cook c)
        {
            this.numOfServings=numOfServings;
            this.id=id;
            this.c=c;
        }
    
        public void run()
        {
            for(int i = 0; i<2; i++)
            {
                getServingsFromPot(id, numOfServings);
            }
        }
    
        public synchronized void getServingsFromPot(int id, int[] numOfServings)
        {
            try{
                while(numOfServings[0]==0)
                {
                    System.out.println("There's no more missionary in the pot!");
                    c.putServingsInPot(numOfServings);
                    wait();
                }
            } catch(Exception e){}
    
            System.out.println("Savage " + id + " getting a serving to eat");
            Timer.eat(id);
            numOfServings[0]--;
            System.out.println("Savage " + id + " finished eating");
        }
    }
    Java Code:
    public class Admin
    {
        public static void main (String[] args)
        {
            
            int[] numOfServings = new int[1];
            Cook cook = new Cook(numOfServings);
            Savage[] savage = new Savage[10];
            
            
            cook.start();        
            for(int i = 0; i<savage.length; i++)
            {
                savage[i] = new Savage(numOfServings, i+1, cook);
                savage[i].start();
            }
            
            
            
            System.out.println("Ending...There are " + numOfServings[0] + " missionarie left in the pot");
        }
    }
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    There's no more missionary in the pot!
    Cook has put 3 missionaries into pot.
    Cook has put 1 missionaries into pot.
    Cook has put 5 missionaries into pot.
    Cook has put 3 missionaries into pot.
    Cook has put 5 missionaries into pot.
    Cook has put 3 missionaries into pot.
    Cook has put 4 missionaries into pot.
    Cook has put 2 missionaries into pot.
    Cook has put 3 missionaries into pot.
    Cook has put 2 missionaries into pot.
    Cook has put 1 missionaries into pot.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: My wait() and notify() wouldnt work.

    Did you read thru the tutorials and try out some examples.? Take some small steps first to see how threads behave. Concurrent programming ins one of the more complex (and difficult to debug) aspects of programming.

    Also, please don't change the title when posting replies.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 11-25-2012, 02:51 AM
  2. jbuttons and jcombobox didn't show up please help...
    By jackwong in forum New To Java
    Replies: 3
    Last Post: 06-08-2012, 04:48 PM
  3. The code didn't delete the text file
    By amyng in forum New To Java
    Replies: 3
    Last Post: 05-01-2012, 06:37 AM
  4. Replies: 0
    Last Post: 02-27-2012, 10:29 PM
  5. My own ClassLoader didn't work.
    By snooze-g in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2007, 11:12 AM

Posting Permissions

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