Results 1 to 7 of 7
  1. #1
    elecrobot is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default Shared variable mutual exclusion problem. Plz help

    Hi Everyone!

    I am very new to Java programming, therefore i apologize for any stupid question and i will really appreciate comments from anyone.

    My problem is I am trying to make a parking facility on java which will have two enterances and two exits. when i run the thread the total ammount of cars in the parking space is not accurate. I guess it is the mutual exclusion problem which i am facing. Now i am trying to solve it using synchronisation. Could anyone please let me know what further steps do i need to take to solve this problem.

    My program structure is i have a class for one shared variable i.e. Total. which is being used by both entrance and exit classes. i think this shared variable i.e. Total needs to by synchronised. My code for entrance and shared variable class is as follows:-

    Shared variable class:-


    public class variable
    {

    public static int Total;

    }


    Entrance class:-

    public class Entrance extends Thread
    {
    int value1;
    int X1;

    variable S;

    public Entrance()
    {
    variable S= new variable();
    }

    public Entrance(int ThreadName,int max)
    {

    X1=ThreadName;
    value1=max;
    }

    public void run()
    {


    for(int Y1=0;Y1<value1;Y1++)
    {


    if(S.Total<50)
    {
    S.Total++;
    System.out.println("Car in at "+X1+", Total =" +S.Total+"{"+(Y1+1)+"}");
    }

    }


    }


    }


    object class:-


    public class objects
    {
    variable S;



    public objects()
    {
    S.Total=0;

    Entrance Entrance1=new Entrance(1,50);
    Entrance1.start();


    Exit Exit1=new Exit(1,50);
    Exit1.start();

    Entrance Entrance2=new Entrance(2,50);
    Entrance2.start();

    Exit Exit2=new Exit(2,50);
    Exit2.start();


    }

    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Random;
    
    public class GarageTest 
    {
        public static void main(String[] args)
        {
            Garage garage = new Garage();
            Entrance entrance1 = new Entrance(garage, 1);
            entrance1.start();
    
            Exit exit1 = new Exit(garage, 1);
            exit1.start();
    
            Entrance entrance2 = new Entrance(garage, 2);
            entrance2.start();
    
            Exit exit2 = new Exit(garage, 2);
            exit2.start();
        }
    }
    
    class Garage
    {
        public static int TOTAL = 10;
        int currentNumber = 0;
    
        public synchronized void enter(int id, int carNumber) {
            while (currentNumber == TOTAL) {
                try {
                    wait();
                } catch (InterruptedException e) { }
            }
            currentNumber++;
            System.out.printf("Car %2d entered at %d  " +
                              "currentNumber = %d%n",
                               carNumber, id, currentNumber);
            notifyAll();
        }
    
        public synchronized void exit(int id) {
            while (currentNumber == 0) {
                try {
                    wait();
                } catch (InterruptedException e) { }
            }
            currentNumber--;
            System.out.println("Car    exited  at " + id +
                               "  currentNumber = " + currentNumber);
            notifyAll();
        }
    }
    
    class Entrance extends Thread
    {
        Garage garage;
        int number;
    
        public Entrance(Garage garage, int threadNum)
        {
            this.garage = garage;
            number = threadNum;
        }
    
        public void run()
        {
            for(int j = 0; j < 10; j++)
            {
                garage.enter(number, j+1);
            }
        }
    }
    
    class Exit extends Thread
    {
        Garage garage;
        int number;
        Random seed = new Random();
    
        public Exit(Garage garage, int threadNum)
        {
            this.garage = garage;
            number = threadNum;
        }
    
        public void run() {
            for (int j = 0; j < 10; j++) {
                garage.exit(number);
                try {
                    sleep(2000 + seed.nextInt(3000));
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }

  3. #3
    elecrobot is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    Thanks hardwired!!!... i found the problem in my program. Could you please let me know why is it necessary to add garage in the constructor below:- Thanks for your help

    public Entrance(Garage garage, int threadNum)
    {
    this.garage = garage;
    number = threadNum;
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    So that the Entrance class could use the Garage reference, viz, "garage" to call the enter method in the Garage class.
    Java Code:
    class Entrance extends Thread
    {
        Garage garage;
    
        public Exit(Garage garage, int threadNum)
        {
            this.garage = garage;
            ...
    
        public void run()
        {
            for(int j = 0; j < 10; j++)
            {
                garage.enter(number, j+1);

  5. #5
    elecrobot is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    gr8 stuff i got it. Now if i make my enter and exit methods static and delete the Garage garage from the constructor, do u suggest it to be a good alternative? well i tried it. it gives me an error that non-static method i.e.notifyAll and wait cannot be used in static.

    its just i want to learn different ways of doing. Could you plz shed some light on this. lets say i definately want to use this alternative method, then how can i do this.

    Kind Regards,

    elecrobot

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Now if i make my enter and exit methods static and delete the Garage garage from the constructor, do u suggest it to be a good alternative?
    No.
    well i tried it. it gives me an error that non-static method i.e.notifyAll and wait cannot be used in static.
    That makes sense. You can look up these methods in the Object class api to see that they are both declared public.
    There may be a number of other ways you can do this. For more about this try Lesson: Concurrency.

  7. #7
    elecrobot is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    absolute great stuff. now i have further played with the program and have found few more question. that would be really kind of you if you plz explain me the following questions:-
    1)if i write garage=garage instead of this.garage=garage, the code still works but total i.e. current number goes into negative. is there any logic behind it or i m making another mistake in my program?
    2)why do we need following code:-
    public static void main(String[] args)..if i replace this with
    public GarageTest()...code still works perfectly fine.
    3)sometimes in method we write void and sometimes don't, what does void really means?
    4)why do we really need currentNumber, why can't we directly use if(Total==50) instead of if(currentNumber==50).

    Thanks a lot for your help. really appreciate.
    Kind Regards,
    elecrobot
    Last edited by elecrobot; 11-26-2007 at 11:59 PM.

Similar Threads

  1. how to access shared file/folder in LAN
    By ksheetiz in forum Networking
    Replies: 1
    Last Post: 05-24-2008, 05:54 AM
  2. Shared Scientific Toolbox in Java 1.00
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-10-2008, 05:16 PM
  3. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 08:23 AM
  4. Shared Scientific Toolbox in Java 0.99
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-16-2008, 09:38 PM
  5. I have a problem with variable "i"
    By silvia in forum New To Java
    Replies: 2
    Last Post: 08-08-2007, 12:05 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
  •