Answer to Question #242866 in Java | JSP | JSF for kavin

Question #242866

The Airplane always needs to check with the Airport to see if it has an

available runway before it's able to take off or land. Simulate the above-

mentioned scenario using multi-threading.


1
Expert's answer
2021-09-27T15:47:26-0400
public class Airport{
    static Runway r1[] = new Runway[20];
    static Plane p1[] = new Plane[30];


    public static void main(String[] args) {
        for (int i = 0; i < p1.length; i++){
            p1[i] = new Plane(i);
        }
        List<Plane> pls = Arrays.asList(p1);
        Iterator<Plane> it;
        Collections.shuffle(pls);
        it= pls.it();
        for (int i = 0; i < r.length; i++) {
            Plane p1;
            try {
                p1= it.next();
            }catch ( RuntimeException e){
                p1= null;
            }
            r1[i] = new Runway(i,p1);
        }


        for (int i = 0; i < p1.length; i++){
            Runway rw=getUsedRunway(p1[i]);
            System.out.println("plane "+p1[i].id + " is "+(rw==null?"waiting to land":("on runway "+r.id)));
        }


        for (int i = 0; i < p1.length; i++){
            p1[i].start();
        }
    }




    private static class Runway {
        private int id;
        private AtomicRef<Plane> pAtomicRef;


        public Runway(int i, Plane p) {
            id =i;
            pAtomicRef = new AtomicRef<>();
            pAtomicRef.set(p);
        }
    }




    private static class Plane extends Thread {
        private int id;


        Plane(int i){
            id=i;
        }


        @Override
        public void run() {
            Runway rw=getUsedRunway(this);
            if(rw==null){
                System.out.println("plane "+id+" wants to land");
                Runway availableRunway = availableRunway();
                while ((availableRunway=atomicallyAttempToLandPlane(this))==null) {
                    System.out.println("no runway available yet for plane " + id);
                    try {
                        sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("landed plane "+id+" on runway "+availableRunway.id);
            }else {
                System.out.println("plane "+id+" wants to take off from runway "+rw.id);
                rw.pAtomicRef.set(null);
                System.out.println("plane "+id+" has taken off from runway ");
            }
        }


        
        private Runway atomicallyAttempToLandPlane(Plane plane) {
            for (int i = 0; i < r.length; i++) {
                if(r[i].pAtomicRef.compareAndSet(null,plane)){
                    return r[i];
                }
            }
            return null;
        }
    }


   
    private static Runway available_runway(){
        for (int i = 0; i < r.length; i++) {
            if(r[i].pAtomicRef.get() ==null){
                return r[i];
            }
        }
        return null;
    }


    private static Runway getUsedRunway(Plane plane){
        for (int i = 0; i < r.length; i++) {
            final Plane planeOnRunway  = r[i].pAtomicRef.get();
            if(planeOnRunway !=null && planeOnRunway.id==plane.id){
                return r[i];
            }
        }
        return null;
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS