stevnlon 0 Newbie Poster

Hi, im trying to get code that if seats are availabile on a bus, then a passenger may get on. For the code i have provided, when i run it ,passenger gets on at the correct stop, but never get off.....i.e. the first 2 passengers ride forever........ help! what am i doing wrong?

private int position;    // bus's currrent position, i.e. bus stop.
private int available;   // available seats
available = Main.CAPACITY; //  located in bus constructor.
public final static int CAPACITY = 2; // located in class main.
-----------------------------------------------------------------------------------

// Method called by passengers wishing to get ON the bus at bus stop


public void getOn(int fromStop)
  {    
    bus.lock();  // passenger obtains the resource.

   try
//passenger waits until the bus is at the bus stop and a seat is available.
   {
       while(fromStop < position || fromStop > position ||
available == Main.CAPACITY-2) 
       {
           wantingOn.await();
       }
       available--;   // passenger gets on the bus
       wantingOff.signal();
   }
       catch (InterruptedException e)
       {}
       finally
       {           
      // passenger releases the resource
           bus.unlock();
        }
    }
   
   


  // Method called by passengers wishing to get off the bus at bus stop.
  public void getOff(int toStop) 
  {
    //  passenger obtains the resource.
  bus.lock();

  try
  {
    // passenger waits until the bus is at the bus stop.
    while(toStop != position)
    {
        wantingOff.await();
    }
    available++;
    wantingOn.signal();
  }
    catch (InterruptedException e)
    {}
    finally
    {
    // passenger releases the resource
        bus.unlock();
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.