Advertisement
Gologo

laba_Kate

Nov 20th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1.  
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4.  
  5.  
  6. public class Main {
  7.     static int NUM_OF_PLANES = 10;
  8.     static int NUM_OF_LINES = 9;
  9.     static long DELAY = 1000;
  10.    
  11.     public static void main(String [] args){
  12.         boolean lines[] = new boolean[NUM_OF_LINES];
  13.         Plane[] arrPlane = new Plane[NUM_OF_PLANES];
  14.        
  15.         for(int i = 0;i<NUM_OF_PLANES;i+=1){
  16.             arrPlane[i] = new Plane(Integer.toString(i),DELAY);
  17.         }
  18.        
  19.         for(int i = 0;i<NUM_OF_LINES;i+=1){
  20.             lines[i] = true;
  21.         }
  22.        
  23.         Runnable r = new Runnable(){
  24.             @Override
  25.             public void run(){
  26.              //   for(int i = 0;i<NUM_OF_PLANES;i+=1){
  27.                     int plane = searchPlane(arrPlane);
  28.                     int line = search_line(lines);
  29.                     if(line != -1&& plane != -1){
  30.                         arrPlane[plane].setStatus(false);
  31.                         lines[line] = false;
  32.                         System.out.println("Самолет"+arrPlane[plane].getName()+"начинает посадку на полосу "+line);
  33.                        
  34.                        
  35.                         try {
  36.                             Thread.sleep(DELAY);
  37.                         } catch (InterruptedException ex) {/*pass*/}
  38.                        
  39.                         System.out.println("Самолет"+arrPlane[plane].getName()+"заканчивает посадку на полосу "+line);
  40.                        
  41.                         synchronized(arrPlane){
  42.                             arrPlane.notifyAll();
  43.                         }
  44.                         lines[line] = true;
  45.                        
  46.                     }else{
  47.                         System.out.println("Самолет"+arrPlane[plane].getName()+"ожидает свободную полосу ");
  48.                         try {
  49.                             synchronized(arrPlane){
  50.                                 arrPlane.wait();
  51.                             }
  52.                         run();
  53.                         } catch (InterruptedException ex) {/*pass*/}
  54.                     }
  55.                 }
  56.             //}
  57.         };
  58.         for(int i =0;i<NUM_OF_PLANES;i+=1){
  59.             Thread t = new Thread(r);
  60.             t.start();
  61.         }
  62.     }
  63.    
  64.     static synchronized int searchPlane(Plane[]arr){
  65.         for(int i = 0;i<NUM_OF_PLANES;i+=1){
  66.             if(arr[i].getStatus() == true){
  67.                 return i;
  68.             }
  69.         }
  70.         return -1;
  71.     }
  72.    
  73.     static synchronized int search_line(boolean[]lines){
  74.         for(int i = 0;i<NUM_OF_LINES;i+=1){
  75.             if(lines[i] == true){
  76.                 return i;
  77.             }
  78.         }
  79.         return -1;
  80.     }
  81. }
  82.  
  83. /*
  84. Второй класс
  85. В отдельный файл
  86. */
  87.  
  88.  
  89. import java.util.logging.Level;
  90. import java.util.logging.Logger;
  91.  
  92.  
  93. public class Plane {
  94.     private long delay = 100;
  95.     private String name;
  96.     private boolean status = true;
  97.     public Plane(String name,long delay){
  98.         this.delay = delay;
  99.         this.name = name;
  100.     };
  101.    
  102.     public synchronized String getName(){
  103.         return name;
  104.     }
  105.  
  106.     public boolean getStatus() {
  107.         return status;
  108.     }
  109.    
  110.     public void setStatus(boolean s){
  111.         status = s;
  112.     }
  113.    
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement