Guest User

start of air port

a guest
Aug 17th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. AirPlane.java
  2. ===================
  3.  
  4. public class AirPlane extends Thread{
  5.     private AirPort myAirPort;
  6.    
  7.    
  8.     public AirPlane(AirPort airPort)
  9.     {
  10.         this.myAirPort=airPort;
  11.     }
  12.    
  13.     public long getTheId()
  14.     {
  15.         return 0;
  16.     }
  17. }
  18.  
  19.  
  20. AirPort.java
  21. ====================
  22. import java.util.LinkedList;
  23. import java.util.Queue;
  24. import java.util.Vector;
  25.  
  26.  
  27. public class AirPort implements Runnable {
  28.     // name of our air port
  29.     private String name;
  30.     // list of all air planes
  31.     private Vector<AirPlane> allAirplanes = new Vector<AirPlane>();
  32.     // list of air plane in queue
  33.     private Queue<AirPlane> waitingAirplanes = new LinkedList<AirPlane>();
  34.     // if my lovely air port is open
  35.     private boolean isOpen = true;
  36.  
  37.     // set my airport name
  38.     public AirPort(String name) {
  39.         this.name = name;
  40.     }
  41.  
  42.     // add new airplane to airport
  43.     public void addAirplane(AirPlane newAirPlane) {
  44.         allAirplanes.add(newAirPlane);
  45.         newAirPlane.start();
  46.     }
  47.  
  48.     // close air port
  49.     public void closeAirport() {
  50.         this.isOpen = false;
  51.         synchronized (this) {
  52.             notifyAll();
  53.         }
  54.     }
  55.  
  56.     // add waiting airplane to the list.
  57.     public synchronized void addWaitingAirplane(AirPlane waitingAirplane) {
  58.         waitingAirplanes.add(waitingAirplane);
  59.         System.out.println("we add plane #" + waitingAirplane.getId() + "there are still " + waitingAirplanes.size()
  60.                 + " air planes waiting");
  61.  
  62.         //dummy waiting for letting us know that there
  63.         //is an airplane waiting
  64.         /*
  65.         synchronized (this) {
  66.             if (waitingAirplanes.size() == 1) {
  67.                 notify();
  68.             }
  69.         }
  70.         */
  71.     }
  72.  
  73.     // notify single air plane
  74.     public synchronized void notifyAirPlane() {
  75.         AirPlane firstAirplane = waitingAirplanes.poll();
  76.         if (firstAirplane != null) {
  77.             System.out.println("air port is notifying airplane #" + firstAirplane.getTheId());
  78.             synchronized (firstAirplane) {
  79.                 firstAirplane.notifyAll();
  80.             }
  81.         }
  82.         try {
  83.             System.out.println(
  84.                     "AirPort waits the airplane #" + firstAirplane.getTheId() + " will announce it is finished");
  85.             wait(); // wait till the airplane is finished
  86.             System.out.println("Airport was announced the airplane #" + firstAirplane.getTheId() + " is finished");
  87.         } catch (InterruptedException e) {
  88.             e.getMessage();
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public void run() {
  94.         // TODO Auto-generated method stub
  95.         System.out.println("Air port is ready.....");
  96.         while (isOpen) {
  97.             if (!waitingAirplanes.isEmpty()) {
  98.                 notifyAirPlane();
  99.             } else {
  100.                 synchronized (this) {
  101.                     try {
  102.                         System.out.println("Air Port has no air planes that waits");
  103.                         wait(); // wait for getting an air plane
  104.                         System.out.println("air port recived a message from waiting airplane");
  105.                     } catch (InterruptedException e) {
  106.                         e.printStackTrace();
  107.                     }
  108.                 }
  109.             }
  110.             System.out.println("Air port is closed for Asakim!!!");
  111.         }
  112.  
  113.     }
  114. }
  115.  
  116.  
  117. tester.java
  118. ================
  119.  
  120. public class Tester {
  121.  
  122.     public static void main(String[] args) throws InterruptedException {
  123.         AirPort ourAirPort = new AirPort("Schifall");
  124.         Thread airPortThread = new Thread(ourAirPort);
  125.         airPortThread.start();
  126.        
  127.         AirPlane[] allAirPlanes = new AirPlane[25];
  128.        
  129.         for (int i=0;i<allAirPlanes.length; i+=1)
  130.         {
  131.             allAirPlanes[i] = new AirPlane(ourAirPort);
  132.             ourAirPort.addAirplane(allAirPlanes[i]);
  133.         }
  134.        
  135.         for (int i=0; i <allAirPlanes.length; i+=1)
  136.         {
  137.             allAirPlanes[i].start();
  138.             allAirPlanes[i].join();
  139.         }
  140.        
  141.         ourAirPort.closeAirport();
  142.         airPortThread.join();
  143.         System.out.println("Good bye!!!!");
  144.        
  145.        
  146.     }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment