Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AirportPkg;
- public class Airplane extends Thread {
- // final state for an airplane
- public enum AirplaneState {
- TakeOff, Land
- }
- private static int autoId = 100;
- private Airport airport;
- private AirplaneState finalState;
- private int flightNo;
- public Airplane(Airport airport) {
- this.airport = airport;
- this.finalState = Utils.getRandomState();
- autoId += 1;
- this.flightNo = Airplane.autoId;
- this.start();
- }
- public AirplaneState getFinalState() {
- return this.finalState;
- }
- public int getFlightNo() {
- return this.flightNo;
- }
- @Override
- public void run() {
- synchronized (this) {
- try {
- // waiting to get permission from airport
- wait();
- int time = this.getTime();
- Utils.printLog("Start", this + " start to " + finalState
- + " for " + time + "ms");
- // airplane is flying. all others should wait
- Thread.sleep(this.getTime());
- Utils.printLog("End", this + " has finished to " + finalState);
- // notify airport to continue
- synchronized (airport) {
- this.airport.notify();
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- // get random time for this airplane to land or takeoff
- private int getTime() {
- if (this.finalState == AirplaneState.Land)
- return Utils.getLandingTime();
- if (this.finalState == AirplaneState.TakeOff)
- return Utils.getTakingOffTime();
- return 0;
- }
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return "Airplane #" + this.flightNo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement