Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AirPlane.java
- ===================
- public class AirPlane extends Thread{
- private AirPort myAirPort;
- public AirPlane(AirPort airPort)
- {
- this.myAirPort=airPort;
- }
- public long getTheId()
- {
- return 0;
- }
- }
- AirPort.java
- ====================
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Vector;
- public class AirPort implements Runnable {
- // name of our air port
- private String name;
- // list of all air planes
- private Vector<AirPlane> allAirplanes = new Vector<AirPlane>();
- // list of air plane in queue
- private Queue<AirPlane> waitingAirplanes = new LinkedList<AirPlane>();
- // if my lovely air port is open
- private boolean isOpen = true;
- // set my airport name
- public AirPort(String name) {
- this.name = name;
- }
- // add new airplane to airport
- public void addAirplane(AirPlane newAirPlane) {
- allAirplanes.add(newAirPlane);
- newAirPlane.start();
- }
- // close air port
- public void closeAirport() {
- this.isOpen = false;
- synchronized (this) {
- notifyAll();
- }
- }
- // add waiting airplane to the list.
- public synchronized void addWaitingAirplane(AirPlane waitingAirplane) {
- waitingAirplanes.add(waitingAirplane);
- System.out.println("we add plane #" + waitingAirplane.getId() + "there are still " + waitingAirplanes.size()
- + " air planes waiting");
- //dummy waiting for letting us know that there
- //is an airplane waiting
- /*
- synchronized (this) {
- if (waitingAirplanes.size() == 1) {
- notify();
- }
- }
- */
- }
- // notify single air plane
- public synchronized void notifyAirPlane() {
- AirPlane firstAirplane = waitingAirplanes.poll();
- if (firstAirplane != null) {
- System.out.println("air port is notifying airplane #" + firstAirplane.getTheId());
- synchronized (firstAirplane) {
- firstAirplane.notifyAll();
- }
- }
- try {
- System.out.println(
- "AirPort waits the airplane #" + firstAirplane.getTheId() + " will announce it is finished");
- wait(); // wait till the airplane is finished
- System.out.println("Airport was announced the airplane #" + firstAirplane.getTheId() + " is finished");
- } catch (InterruptedException e) {
- e.getMessage();
- }
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("Air port is ready.....");
- while (isOpen) {
- if (!waitingAirplanes.isEmpty()) {
- notifyAirPlane();
- } else {
- synchronized (this) {
- try {
- System.out.println("Air Port has no air planes that waits");
- wait(); // wait for getting an air plane
- System.out.println("air port recived a message from waiting airplane");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- System.out.println("Air port is closed for Asakim!!!");
- }
- }
- }
- tester.java
- ================
- public class Tester {
- public static void main(String[] args) throws InterruptedException {
- AirPort ourAirPort = new AirPort("Schifall");
- Thread airPortThread = new Thread(ourAirPort);
- airPortThread.start();
- AirPlane[] allAirPlanes = new AirPlane[25];
- for (int i=0;i<allAirPlanes.length; i+=1)
- {
- allAirPlanes[i] = new AirPlane(ourAirPort);
- ourAirPort.addAirplane(allAirPlanes[i]);
- }
- for (int i=0; i <allAirPlanes.length; i+=1)
- {
- allAirPlanes[i].start();
- allAirPlanes[i].join();
- }
- ourAirPort.closeAirport();
- airPortThread.join();
- System.out.println("Good bye!!!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment