-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirTrafficController.java
executable file
·54 lines (45 loc) · 1.9 KB
/
AirTrafficController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Author: Arslan Khurram
//Program: Airport Simulation using threads
//Date: 24th Feb 2022
//Email: [email protected]
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class AirTrafficController {
public static void main(String[] args) {
//Used to generate Status of Aircraft, Whether it will be landing or taking off
Random random = new Random();
//Functions the same way as arrayblockingqueue but unlike arrayblockingqueue it can be optionally-bounded.
BlockingQueue<Runway> runways = new LinkedBlockingQueue<Runway>(2);
BlockingQueue<Gate> gates = new LinkedBlockingQueue<Gate>(2);
//These thread pools will used for runway and gate, respectively.
//After plane has successfully landed then it give the aircraft to the gate.
//After Gate does his thing it will give it back the runway for take off.
ExecutorService runwayThreadPool = Executors.newFixedThreadPool(2);
ExecutorService gateThreadPool = Executors.newFixedThreadPool(2);
//creating gates and runways objects
for(int i = 1; i <= 2; i++) {
runways.add(new Runway(i));
gates.add(new Gate(i));
}
//contruct 10 aircrafts according to the requirements and submitting them to the thread pool
for (int i = 1; i <= 10; i++) {
try {
Thread.sleep(random.nextInt(5001)); //In assignment Question
runwayThreadPool.submit(new Plane(i, random.nextBoolean(), runways, gates));
}catch (InterruptedException e) {
e.printStackTrace(); //error handling method
}
}
gateThreadPool.shutdown();
runwayThreadPool.shutdown();
try {
if (runwayThreadPool.awaitTermination(500, TimeUnit.SECONDS)) {
System.out.println("Simulation terminated !");
}
} catch (InterruptedException e) {}
}
}