Threads with multi shared resources

441 views Asked by At

I'm having some trouble on a Java college project.

So, the project's about an airport (server) and clients that send objects (airplanes) to simulate its arrivals on two or more landing tracks. I'm not with the project code at hand so I'll try to explain myself.

Now, my problem's not the distributed part of the project. My real problem's on the concurrency.

So, obviously, only one airplane at a time is allowed to land on each one of the tracks.

Threads (airplanes) calls an airport method

  (askLanding(Airplane ap)) 

btw, idk if it's possible for a thread to call a method which receives the same thread as an argument

and the airport manages the landings by going through every track of the airport (loop) and check if they're open for landing (i got the option to open/close whenever i want) to land and then calls another method, a synchronized one:

  (tracks.get(i).toLand(ap))

If the track's being used by another plane, the thread will wait().

My issue's if the first track is open, every plane will wait on the first track and the other tracks will be empty. If i check the tracks availability on the askLanding() method I'm locking the shared resource and its tracks (airport). Even though the airplane is only using one of the tracks to land, no other airplane can land on any of the other tracks.

I feel I'm doing a huge mistake somewhere, but I just can't see it. As soon as I can, I'll paste some code here.

EDIT: Ok, here it is some code:

Aircraft Thread

public class Aircraft extends Thread{

protected int tankCap;
protected int curFuel;
protected int flightNum;
protected int fuelCons;
protected Airport ap1;
protected boolean landed;
protected int time;
protected int priority;

public Aircraft(Airport ap1, int comb) {
    this.ap1 = ap1;
    flightNum = new Random().nextInt(300);
    this.curFuel = comb;
    this.landed = false;        
}

@Override
public void run() {
    ap1.reqLanding(this);
}

Airport Class

public class Airport {

private LinkedList<Runway> runways;
private LinkedList<Aircraft> aircraftsToLand;



public Airport() {

    runways = new LinkedList<Runway>();
    aircraftsToLand = new LinkedList<Aircraft>();


}


public void reqLanding(Aircraft a){

    aircraftsToLand.add(a);

    for(int i = 0; i != runways.size(); i++){

        if(runways.get(i).getOpen()){
            runways.get(i).land(a);
        }
    }

}

Runway Class - Land Method

public synchronized void land(Aircraft a){
    numLanding++;

    while(available == false){

        try {
            a.setFuel(a.getCurFuel() - a.getFuelCons());
            System.out.println(a.toString());
            wait();
        } catch (InterruptedException e) {
            System.out.println("Interrupted while on wait");
        }
    }
    available = false;
    runwayTxt.setText(a.toString());

    System.out.println(getnPista().getText() + " A aterrar:" + a.toString());
    try {
        Thread.sleep(6000);
    } catch (InterruptedException e) {
    }
    System.out.println(getnPista().getText() + " Aterrou:" + a.toString());

    runwayTxt.setText("");
    numLanding--;
    a.setLanding(true);
    available = true;
    notifyAll();
}

Thanks

0

There are 0 answers