-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.java
177 lines (145 loc) · 4.87 KB
/
Process.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cpuschedulingsimulator;
import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class Process
{
//DECLARING THE ATTRIBUTES OF PROCESSES
private String processID;
private int burstTime;
private int arrivalTime;
private int currentBurstTime;
private boolean isCompleted;
private JLabel remainingBurstTime;
private JProgressBar count;
private int pRemainingBurstTime;
private int waitTime;
private int turnAroundTime;
private Color color;
//CONSTRUCTOR
public Process(String processID, int burstTime, int arrivalTime, JLabel remainingBurstTime, JProgressBar count) {
this.processID = processID;
this.burstTime = burstTime;
this.arrivalTime = arrivalTime;
this.currentBurstTime = 0;
this.isCompleted = false;
this.remainingBurstTime = remainingBurstTime;
this.count = count;
this.pRemainingBurstTime = 0;
this.waitTime = 0;
this.turnAroundTime = 0;
this.color = randomColorMaker();
}
//GETTERS AND SETTERS
public int getID() {
String getChar = "";
for (char c: this.processID.toCharArray()) {
if (Character.isDigit(c)) {
getChar = getChar + c;
}
}
return Integer.parseInt(getChar); //CONVERTING GUI COMPONENT'S PID(STRING) TO INTEGER
}
public int getBurstTime() {
return burstTime;
}
public void setBurstTime(int burstTime) {
this.burstTime = burstTime;
}
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getCurrentBurstTime() {
return currentBurstTime;
}
public void setCurrentBurstTime(int currentBurstTime) {
this.currentBurstTime = currentBurstTime;
checkCompletion();
updateGUI();
}
public boolean getIsCompleted() {
return isCompleted;
}
public void setIsCompleted(boolean isCompleted) {
this.isCompleted = isCompleted;
}
public int getpRemainingBurstTime() {
return pRemainingBurstTime;
}
public void setpRemainingBurstTime(int pRemainingBurstTime) {
this.pRemainingBurstTime = pRemainingBurstTime;
}
public int getWaitTime() {
return waitTime;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
public int getTurnAroundTime() {
return turnAroundTime;
}
public void setTurnAroundTime(int turnAroundTime) {
this.turnAroundTime = turnAroundTime;
}
public String getProcessID() {
return processID;
}
public void setProcessID(String processID) {
this.processID = processID;
}
public JLabel getRemainingBurstTime() {
return remainingBurstTime;
}
public void setRemainingBurstTime(JLabel remainingBurstTime) {
this.remainingBurstTime = remainingBurstTime;
}
public JProgressBar getCount() {
return this.count;
}
public void setCount(JProgressBar count) {
this.count = count;
}
public Color getColor() {
return this.color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isCompleted() {
return this.isCompleted;
}
public void setCompleted(boolean isCompleted) {
this.isCompleted = isCompleted;
}
//GENERATE RANDOM COLOR FOR EACH PROCESS FOR GANTT CHART
private static Color randomColorMaker() {
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
return new Color(red, green, blue);
}
//EACH TIME A THREAD OF PROCESS RUNS, IT'S CURRENT BURTST (INITIALLY 0) INCREMENTS TILL IT IS LESS THAN BURST TIME OF PROCESS
public void run() {
if (this.currentBurstTime < this.burstTime) {
this.currentBurstTime++;
}
checkCompletion();
updateGUI();
}
//AS SOON AS CURRENT BURST TIME OF PROCESS BECOMES GREATER THAN BURST TIME OF PROCESS, IT INDICATES THAT PROCESS IS COMPLETED
private void checkCompletion() {
this.isCompleted = (this.currentBurstTime >= this.burstTime);
}
//UPDATE GUI FOR SIMULATION
//FOR EACH STEP OF SIMULATION, REMAINING BURST TIME IS UPDATED
private void updateGUI() {
this.pRemainingBurstTime = this.burstTime - this.currentBurstTime;
this.remainingBurstTime.setText(String.valueOf(this.pRemainingBurstTime));
this.count.setValue(this.currentBurstTime);
}
}