forked from Saurabh-Bhavsar/ATCSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runway.java
119 lines (110 loc) · 4.72 KB
/
Runway.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Runway {
// Atomic object variables to access different resources
public static final Gate1 g = new Gate1();
public static final Gate2 g1 = new Gate2();
public static final ReentrantLock lock = new ReentrantLock();
static int completed = 0;
// this is the method where we make use of fine grained locking using Reentrant
// lock
public void accessRunway() throws InterruptedException {
int AirplaneObjectId = Integer.parseInt(Thread.currentThread().getName());
Airplane workOn = Main.tracker[AirplaneObjectId];
/*
* System.out.println(" Thread -- " + workOn.getName() + ", State -- " +
* workOn.getCurrentStateName(workOn.getCurrentState()));
*/
// if condition is when the Plane is landing
if (workOn.getCurrentState() == 0) {
SwingUI.updateGUIState(workOn.getCurrentStateName(workOn.getCurrentState()),
workOn.getCurrentStateName(workOn.getCurrentState() + 1), AirplaneObjectId);
// SwingUI.model.setValueAt(workOn.getCurrentStateName(workOn.getCurrentState()),
// AirplaneObjectId, 1);
lock.lock();
workOn.setCurrentState(1);
// SwingUI.model.setValueAt(workOn.getCurrentStateName(workOn.getCurrentState()),
// AirplaneObjectId, 1);
SwingUI.updateGUIState(workOn.getCurrentStateName(workOn.getCurrentState()),
workOn.getCurrentStateName(workOn.getCurrentState() + 1), AirplaneObjectId);
SwingUI.updateResourceUsedBy("R", AirplaneObjectId);
try {
// once a thread acquires the lock, we make it sleep for 3 seconds, i.e time
// given for our Runway
Thread.sleep(3000);
} finally {
// Once the thread wakes up, we make a call to acquire either of the Gate
// resource depending on the resource which is free
// or has less threads waiting for it
if (!Gate1.lockG.isLocked()) {
// System.out.println("In if condition");
lock.unlock();
g.accessGate(new Random().nextInt(7));
} else if (!Gate2.lockG1.isLocked()) {
// System.out.println("Else if");
lock.unlock();
g1.accessGate1(new Random().nextInt(7));
} else {
// System.out.println("Else");
workOn.setCurrentState(2);
// SwingUI.model.setValueAt(workOn.getCurrentStateName(workOn.getCurrentState()),
// AirplaneObjectId, 1);
SwingUI.updateGUIState(workOn.getCurrentStateName(workOn.getCurrentState()),
workOn.getCurrentStateName(workOn.getCurrentState() + 1), AirplaneObjectId);
int curr_count_G = Gate1.lockG.getQueueLength();
int curr_count_G1 = Gate2.lockG1.getQueueLength();
if (curr_count_G > curr_count_G1) {
lock.unlock();
g1.accessGate1(new Random().nextInt(6) + 1);
} else {
lock.unlock();
SwingUI.resetResourceTable(0);
g.accessGate(new Random().nextInt(6) + 1);
}
}
}
// System.out.println("Completed -- " + Thread.currentThread().getName());
} else { // else condition is for when the thread is returning from gate and ready for
// takeoff
/*
* date = new Date(); System.out.println("Completed execution - " +
* Thread.currentThread().getName() + " Time --" + dateFormat.format(date));
*/
// System.out.println(" Taking OFF Thread -- " +
// Thread.currentThread().getName());
lock.lock();
workOn.setCurrentState(5);
// SwingUI.model.setValueAt(workOn.getCurrentStateName(workOn.getCurrentState()),
// AirplaneObjectId, 1);
SwingUI.updateGUIState(workOn.getCurrentStateName(workOn.getCurrentState()),
workOn.getCurrentStateName(workOn.getCurrentState() + 1), AirplaneObjectId);
SwingUI.updateResourceUsedBy("R", AirplaneObjectId);
try {
Thread.sleep(3000);
} finally {
lock.unlock();
SwingUI.resetResourceTable(0);
workOn.setCurrentState(6);
}
long time_taken = (new Date().getTime() - workOn.getBeginTimeLongFormat());
// Here the process is completed
System.out.println("Thread -- " + Thread.currentThread().getName()
+ " Took off successfully, Total Time Taken --- " + time_taken / 1000 + " seconds");
String finish = "Took off successfully, Total time taken " + time_taken / 1000 + " seconds";
// SwingUI.resetResourceTable();
// SwingUI.model.setValueAt(finish, AirplaneObjectId, 1);
SwingUI.updateGUIState(finish, "--", AirplaneObjectId);
SwingUI.updateEndTime(new Date(), AirplaneObjectId);
/*
* completed++; if(completed == Main.tracker.length) System.exit(0);
*/
}
}
}