Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Main {
- static int NUM_OF_PLANES = 10;
- static int NUM_OF_LINES = 9;
- static long DELAY = 1000;
- public static void main(String [] args){
- boolean lines[] = new boolean[NUM_OF_LINES];
- Plane[] arrPlane = new Plane[NUM_OF_PLANES];
- for(int i = 0;i<NUM_OF_PLANES;i+=1){
- arrPlane[i] = new Plane(Integer.toString(i),DELAY);
- }
- for(int i = 0;i<NUM_OF_LINES;i+=1){
- lines[i] = true;
- }
- Runnable r = new Runnable(){
- @Override
- public void run(){
- // for(int i = 0;i<NUM_OF_PLANES;i+=1){
- int plane = searchPlane(arrPlane);
- int line = search_line(lines);
- if(line != -1&& plane != -1){
- arrPlane[plane].setStatus(false);
- lines[line] = false;
- System.out.println("Самолет"+arrPlane[plane].getName()+"начинает посадку на полосу "+line);
- try {
- Thread.sleep(DELAY);
- } catch (InterruptedException ex) {/*pass*/}
- System.out.println("Самолет"+arrPlane[plane].getName()+"заканчивает посадку на полосу "+line);
- synchronized(arrPlane){
- arrPlane.notifyAll();
- }
- lines[line] = true;
- }else{
- System.out.println("Самолет"+arrPlane[plane].getName()+"ожидает свободную полосу ");
- try {
- synchronized(arrPlane){
- arrPlane.wait();
- }
- run();
- } catch (InterruptedException ex) {/*pass*/}
- }
- }
- //}
- };
- for(int i =0;i<NUM_OF_PLANES;i+=1){
- Thread t = new Thread(r);
- t.start();
- }
- }
- static synchronized int searchPlane(Plane[]arr){
- for(int i = 0;i<NUM_OF_PLANES;i+=1){
- if(arr[i].getStatus() == true){
- return i;
- }
- }
- return -1;
- }
- static synchronized int search_line(boolean[]lines){
- for(int i = 0;i<NUM_OF_LINES;i+=1){
- if(lines[i] == true){
- return i;
- }
- }
- return -1;
- }
- }
- /*
- Второй класс
- В отдельный файл
- */
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Plane {
- private long delay = 100;
- private String name;
- private boolean status = true;
- public Plane(String name,long delay){
- this.delay = delay;
- this.name = name;
- };
- public synchronized String getName(){
- return name;
- }
- public boolean getStatus() {
- return status;
- }
- public void setStatus(boolean s){
- status = s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement