Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was designing the airport simulation. After the planes had accessed the runway and landed at the gate, I want it to release the runway where other planes are able to access the runway one at a time.

What I have tried:

package assignment_self;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.OptionalInt;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Runway {
    
    final Deque<Integer> availableGates = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4));
    
    OptionalInt claimGate() {
        synchronized (availableGates) {
        if (availableGates.isEmpty()) {
                return OptionalInt.empty();
        }
        return OptionalInt.of(availableGates.remove());
        }
    }
    
    void releaseGate(int gateNumber) {
        synchronized (availableGates) {
            availableGates.add(gateNumber);
        }
    }
    
    void required_to_land(Plane plane) throws InterruptedException
    {
        Thread.sleep((long)(Math.random() * 3000));
        System.out.println("Plane "+ plane.id + " is requesting to land!");
        
        Thread.sleep(500);
        
        access_runway(plane);
    }
    
    synchronized void access_runway(Plane plane) throws InterruptedException
    {
        OptionalInt maybeGateNumber = claimGate();
        
        if (!maybeGateNumber.isPresent()) {
            System.out.println("ATC: All gateways are occupy! Please wait for a moment Plane "+ plane.id);
            return;
        }
        
        System.out.println("Plane " + plane.id + " had accessed the runway!");
        Thread.sleep(1000);
        int gateNumber = maybeGateNumber.getAsInt();
        access_gate(plane, gateNumber);
    }
    
    void access_gate(Plane plane, int gateNumber) throws InterruptedException
    {
        System.out.println("Plane " + plane.id+ " has landed at gate " + gateNumber + "!");
        Thread.sleep(new Random().nextInt(5)*1000);
        System.out.println("Plane " + plane.id+ " had left at gate " + gateNumber + "!");
        
        releaseGate(gateNumber);
    }
}

public class Plane extends Thread{
    Runway runway;
    int id;
    
    Plane(int id, Runway runway)
    {
        this.id = id;
        this.runway = runway;
    }
    
    @Override
    public void run()
    {
        try {
            runway.required_to_land(this);
            
        } catch (InterruptedException ex) {
            Logger.getLogger(Plane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        Runway runway= new Runway();
        
        for(int i=1 ; i<= 10 ; i++)
        {
            Plane p = new Plane(i, runway);
            p.start();
        }
    }
}


This is my current output, after the plane had left the gate, only it release the runway. But what I want is that after the plane had landed at the gate, it will release the runway where other planes are able to access but not wait until the plane left the gate.

Plane 7 is requesting to land!
Plane 1 is requesting to land!
Plane 7 had accessed the runway!
Plane 9 is requesting to land!
Plane 2 is requesting to land!
Plane 8 is requesting to land!
Plane 5 is requesting to land!
Plane 6 is requesting to land!
Plane 4 is requesting to land!
Plane 7 has landed at gate 1!
Plane 7 had left at gate 1!
Plane 8 had accessed the runway!
Plane 3 is requesting to land!
Plane 10 is requesting to land!
Plane 8 has landed at gate 2!
Plane 8 had left at gate 2!
Plane 2 had accessed the runway!
Plane 2 has landed at gate 3!
Plane 2 had left at gate 3!
Plane 9 had accessed the runway!
Plane 9 has landed at gate 4!
Plane 9 had left at gate 4!
Plane 1 had accessed the runway!
Plane 1 has landed at gate 1!
Plane 1 had left at gate 1!
Plane 10 had accessed the runway!
Plane 10 has landed at gate 2!
Plane 10 had left at gate 2!
Plane 3 had accessed the runway!
Plane 3 has landed at gate 3!
Plane 3 had left at gate 3!
Plane 4 had accessed the runway!
Plane 4 has landed at gate 4!
Plane 4 had left at gate 4!
Plane 6 had accessed the runway!
Plane 6 has landed at gate 1!
Plane 6 had left at gate 1!
Plane 5 had accessed the runway!
Plane 5 has landed at gate 2!
Plane 5 had left at gate 2!
Posted
Comments
Richard MacCutchan 17-Sep-21 4:05am    
You need to add code to the access_gate method to release it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900