The Airplane always needs to check with the Airport to see if it has an
available runway before it's able to take off or land. Simulate the above-
mentioned scenario using multi-threading.
package com.company;
import java.util.*;
class my_airport
{
Scanner sc = new Scanner(System.in);
//setting the number of runways
public int[] set_runway()
{
System.out.println("Setting my airport run ways");
System.out.println("Enter number of run ways in the airport");
int number_of_runways = sc.nextInt();
int run_ways[] = new int[number_of_runways];
System.out.println("please, if runaway is available enter 1 else enter any integer");
for(int i = 0; i<number_of_runways; i++)
{
System.out.println("Enter the availability of runway "+(i+1));
run_ways[i] = sc.nextInt();
}
return run_ways;
}
}
public class Main {
static Scanner sc = new Scanner((System.in));
public static void main(String[] args) throws InterruptedException {
//create an object of the class
my_airport my_object = new my_airport();
int choice;
System.out.println("Enter your option :");
System.out.println("1.CHECK AVAILABILITY OF RUNAWAY");
System.out.println("2.EXIT");
choice = sc.nextInt();
switch (choice)
{
case 1: Thread add = new Thread(new Runnable() {
@Override
public void run() {
synchronized (my_object)
{
//declare an array to store the runaways and their availability
int run_way_availability_array[] = my_object.set_runway();
//check there is any run way whose value is 1
int number_of_runways = run_way_availability_array.length;
int available = 0;
//check the run way available for landing
for(int i = 0; i<number_of_runways; i++)
{
if(run_way_availability_array[i]==1)
{
System.out.println("Runway "+(i+1)+" available for landing");
//set available = 1
available = 1;
//if you get any available runway just notify the pilot the runway number and exit the program
break;
}
}
if(available==0)
{
System.out.println("No run way is available for landing");
}
}
}
});
add.start();
add.join();
break;
case 2: Thread sub = new Thread(new Runnable() {
@Override
public void run() {
synchronized (my_object)
{
System.out.println("Program exited successfully");
}
}
});
sub.start();
sub.join();
break;
default:
System.out.println("Invalid option, please try again");
}
}
}
Comments
Leave a comment