Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The getAServing() method is in the Savage class. Each instance of that class has its own monitor. There is no conflict between each thread calling its own methods.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0
    Manage to solve this. Posting this to share with those who are reading this forum to learn something.


    Code:
    public class Admin
    {
        public static void main(String[] args)
        {
            Cook cook = new Cook();
            Savage[] savage = new Savage[10];
            int[] numOfServings = new int[1];
    
            numOfServings[0] = cook.putServingsInPot();
    
            for(int i = 0; i<savage.length; i++)
            {
                savage[i] = new Savage(i+1, numOfServings, cook);
                savage[i].start();
            }
    
            for(int i = 0; i<savage.length; i++)
            {
                try{
                    savage[i].join();
                } catch(InterruptedException e){}
            }
    
            System.out.println("There are " + numOfServings[0] + 
                " missionaries left in the pot.");
        }
    }


    Code:
    public class Cook
    {
        public int putServingsInPot()
        {
            int m = (int)(Math.random()*5)+1;
            System.out.println("Cook has put " + 
                m + " missionaries into pot");
            try
            {
                Thread.sleep(m*500);
            } catch (InterruptedException e){}
            return m;
        }
    }


    Code:
    public class Savage extends Thread
    {
        int id;
        int[]numOfServings;
        Cook c;
    
        public Savage(int id, int[] numOfServings, Cook c)
        {
            this.id=id;
            this.numOfServings=numOfServings;
            this.c=c;
        }
    
        public void getAServing()
        {
            synchronized (numOfServings){
                if(numOfServings[0]==0)
                {
                    System.out.println("There are no more " + 
                        "missionaries in the pot!");
                    numOfServings[0]+=c.putServingsInPot();
                }
    
                System.out.println("Savage " + id + 
                    " getting a serving to eat.");
                numOfServings[0]--;
            }
        }
    
        public void run()
        {
            getAServing();
            try
            {
                Thread.sleep((int)(Math.random()*100)+100);
            } catch (InterruptedException e){}
            System.out.println("Savage " + id + " finished eating.");
        }
    }
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo