Results 1 to 4 of 4

Thread: Wait() Question

  1. #1
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Wait() Question

    Hi All,

    I have a question regarding wait() or sleep().

    The scenario is a cable car between a gorge.
    People from both sides arrive to get across.

    If you arrive to the left side but the car is in the right side or crossing you must wait. In the synchronized method boardCableCar(), while statement you wait() till it gets to your side. An If statement in the same method and checks the CableCar location and then lets you on if it's on your side.

    The issue I have is that the cable only has a limit to carry 15 people.
    How can I board enough people (some might stay on from the trip across) up to the limit and leave the rest in line. I already have a wait() for when the cablecar is not on my side. Can you put another wait() in the method to say the cablecar is on my side, but it already has 15 people on.

    I can get this to work to a certain extent but when the cablecar comes back to me again I am forgotten and do not board. I have not included this code.
    I basically had an additional while statement or a nest IF. Both let me stand in line and not board, but when the cablecar came back I did not get on.

    Thanks in adavance.

    Java Code:
       public synchronized void boardCableCar(Customer c)
       {
          while(c.getside() != cableCarDirection)
          {
             try
             {
                wait();
             }
             catch (Exception e)
             {
                System.out.println(e.getMessage());
             }
          }
          
          if(c.getside() == cableCarDirection)
          {
             System.out.println("Welcome aboard Customer " + c.getName());
             journeyList.add(c);
             
          }
          
       }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't use sleep but wait() on a single object like this:

    Java Code:
    public void embark(CableCar car) {
       synchronized(car.getLock()) {
          while (car.getLocation != MYSIDE || car.getPassengers() == FULL)
             car.getLock().wait();
          car.embark(this); // this passenger enters the car
       }
    }
    The getLock() method of the CableCar can return any object it wants (even itself) but it has to return the same object always. When a CableCar changes its state (e.g. it reaches one of the sides) it does a notifyAll() on that object.

    kind regards,

    Jos

  3. #3
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks Jos for the quick reply.
    As you say I don't want to use sleep or I will block the cableCar condition.
    I have something similar to your while but easier for me to understand. I have done so many different combinations it just seems a bit simple really. I think I tend to over complicate my code.
    I do have it working. Is it possible to have 2 wait() statements in one method. They would of course be in different conditions?

    Thanks again.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rsvr View Post
    I have something similar to your while but easier for me to understand. I have done so many different combinations it just seems a bit simple really. I think I tend to over complicate my code.
    I do have it working. Is it possible to have 2 wait() statements in one method. They would of course be in different conditions?

    Thanks again.
    My code hint also uses two conditions: if the car is not on my side or if it is full, I have to wait. Of course disembarking passengers have to update the state of the car and notifyAll() waiting passengers to give them a chance to embark().

    kind regards,

    Jos

Similar Threads

  1. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 12:58 PM
  2. System.wait() or something of the sort
    By HackerOfDoom in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 10:56 PM
  3. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 09:28 AM
  4. Thread Wait
    By jiexx in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 06:26 PM
  5. How to use sleep() to wait for a while
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:32 PM

Posting Permissions

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