Advertisement
Guest User

my air port city

a guest
Sep 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. AirPlane.java
  2. ==================
  3. package MyTeufaBechakira;
  4.  
  5. import java.util.Random;
  6.  
  7. public class AIrPlane extends Thread{
  8.     private static Object control = new Object(); //the migdal pickuch
  9.     private static int flightNum=1000;
  10.     private int planeNum;
  11.  
  12.     public AIrPlane(){
  13.         this.planeNum=++flightNum;
  14.     }
  15.  
  16.     @Override
  17.     public void run() {
  18.         takeOff();
  19.         fly();
  20.         land();
  21.     }
  22.  
  23.     private void land() {
  24.         System.out.println(planeNum+" wants to land");
  25.         synchronized (control){
  26.             System.out.println(planeNum+" is landing");
  27.         }
  28.         System.out.println(planeNum+" is landed");
  29.     }
  30.  
  31.     private void fly() {
  32.         Random r = new Random();
  33.         int flightTime = r.nextInt(1500)+1500;
  34.         System.out.println(planeNum+" is flying for "+flightTime+"ms");
  35.         try {
  36.             Thread.sleep(flightTime);
  37.         } catch (InterruptedException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.  
  42.     private void takeOff() {
  43.         System.out.println(planeNum+" want to take off");
  44.         synchronized (control){
  45.             System.out.println(planeNum+" is taking3 off");
  46.         }
  47.     }
  48. }
  49.  
  50.  
  51. AirPort.java
  52. ================
  53. package MyTeufaBechakira;
  54.  
  55. import java.util.Scanner;
  56.  
  57. public class AirPort {
  58.     public static void main(String[] args) throws InterruptedException{
  59.         Scanner scanner = new Scanner(System.in);
  60.         System.out.println("How many planes?");
  61.         int totalPlane = scanner.nextInt();
  62.  
  63.         AIrPlane[] planes = new AIrPlane[totalPlane];
  64.         for (int counter=0;counter<planes.length;counter++){
  65.             planes[counter] = new AIrPlane();
  66.             planes[counter].start();
  67.         }
  68.  
  69.         for (int counter=0;counter<planes.length;counter++){
  70.             planes[counter].join();
  71.         }
  72.  
  73.         System.out.println("air port is closed");
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement