-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialExecution.java
428 lines (341 loc) · 10 KB
/
SerialExecution.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//Manish Mahalwal 2016054
//Raj Kamal yadav 2016076
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class DatabaseSerial
{
ArrayList<Flight> Pl;
ArrayList<Passenger> PL;
RockLockS lock = new RockLockS();
public DatabaseSerial(){
Pl=new ArrayList<>();
PL=new ArrayList<>();
Pl.add(new Flight("1"));
Pl.add(new Flight("2"));
Pl.add(new Flight("3"));
Pl.add(new Flight("4"));
Pl.add(new Flight("5"));
}
public void add(Flight t,Passenger a){
int y=Integer.parseInt(t.id)-1;
this.Pl.get(y).Plist.add(a);
}
public void add_f(Flight flight, Passenger a) {
a.Book_flight_list.add(flight);
int psa=search(a.id);
if(psa<0){
PL.add(a);
}
}
public void delete(Flight t,Passenger s){
int yes=0;
for(int i=0;i<t.Plist.size();i++)
{
if(t.Plist.get(i).id.equals(s.id)){
t.Plist.remove(i);
yes=1;
}
}
if(yes==0){
//System.out.println("No Passenger with the given name exist on flight");
}
}
public void delete_f(Flight t,Passenger s){
for(int i=0;i<t.Plist.size();i++)
{
if(s.Book_flight_list.get(i).id.equals(t.id)){
s.Book_flight_list.remove(i);
}
}
}
public int search_on_flight(Flight t,String s){
for(int i=0;i<t.Plist.size();i++)
{
if(t.Plist.get(i).id.equals(s)){
return i;
}
}
return -1;
}
public int search(String s){
for(int i=0;i<PL.size();i++)
{
if(PL.get(i).id.equals(s)){
return i;
}
}
return -1;
}
public void replace(Passenger h,Flight a,Flight b){
for(int l=0;l<h.Book_flight_list.size();l++){
if(h.Book_flight_list.get(l).id.equals(a.id)){
h.Book_flight_list.remove(l);
}
}
h.Book_flight_list.add(b);
}
public void f_lock() throws InterruptedException{
for(int y=0;y<this.Pl.size();y++){
//System.out.println("FLight Id : "+ this.Pl.get(y).id);
//System.out.println("Reserved Passngers : ");
for(int h=0;h<this.Pl.get(y).Plist.size();h++){
//System.out.println(this.Pl.get(y).Plist.get(h).id);
}
}
}
}
class Flight {
String id;
ArrayList<Passenger> Plist;
Lock lock = new ReentrantLock();
public Flight(String t){
id=t;
Plist=new ArrayList<>();
}
public void c_lock(DatabaseSerial dB, Passenger t2) throws InterruptedException{
dB.delete(this, t2);
}
public void h_lock(DatabaseSerial db,Passenger e) throws InterruptedException {
db.delete(this, e);
}
public void i_lock(DatabaseSerial dB, Passenger e) throws InterruptedException {
dB.add(this, e);
}
}
class Passenger implements Serializable {
String id;
ArrayList<Flight> Book_flight_list;
Lock lock=new ReentrantLock();
public Passenger(String t){
id=t;
Book_flight_list=new ArrayList<>();
}
public void d_lock(DatabaseSerial db, Flight flight) throws InterruptedException {
db.delete_f(flight, this);
}
public void e_lock(DatabaseSerial db,int e) throws InterruptedException {
//System.out.println("printing it "+e+" "+db.PL.get(e).Book_flight_list.size());
for(int i=0;i<db.PL.get(e).Book_flight_list.size();i++){
//System.out.println(db.PL.get(e).Book_flight_list.get(i).id);
}
}
public void g_lock(DatabaseSerial db,Flight a,Flight b) throws InterruptedException {
db.replace(this, a, b);
}
}
class RockLockS {
int s_status=0;
//1 is already locked
//0 object can obtain lock
public void lock(){
if(s_status==1){
while(s_status==1){
//System.out.println("x_lock already acquired");
}
//System.out.println("s_lock acquired");
s_status = 1;
}
else
s_status = 1;
}
public void unlock(){
if(s_status==1){
s_status=0;
}
}
}
class TransactionsSerial implements Runnable {
DatabaseSerial DB;
int trans_id;
String flight1, flight2, passenger;
int timestamp=0;
public TransactionsSerial(DatabaseSerial c,int id) throws ClassNotFoundException, IOException{
this.trans_id = id;
DB=c;
}
public TransactionsSerial(DatabaseSerial c,String f,String p, int id) {
this.trans_id = id;
this.flight1 = f;
this.passenger = p;
DB=c;
}
public TransactionsSerial(DatabaseSerial c,String f1,String f2,String p, int id) {
this.trans_id = id;
this.flight1 = f1;
this.flight2 = f2;
this.passenger = p;
DB=c;
}
public TransactionsSerial(DatabaseSerial c,String p, int id) {
this.trans_id = id;
this.passenger = p;
DB=c;
}
public void reserve(String f,String p){
int f_id=Integer.parseInt(f)-1;
int wh=DB.search(p);
if(f_id<5&& f_id>=0 ){
if(wh>=0){
DB.lock.lock();
DB.add_f(DB.Pl.get(f_id), DB.PL.get(wh));
DB.add(DB.Pl.get(f_id), DB.PL.get(wh));
DB.lock.unlock();
}
else{
////System.out.println("therajskldfdsh");
DB.lock.lock();
Passenger t_n=new Passenger(p);
DB.add_f(DB.Pl.get(f_id), t_n);
DB.add(DB.Pl.get(f_id),t_n);
DB.lock.unlock();
}
}
else{
//System.out.println("Invalid Flight ID here");
}
}
public void cancel(String f,String p) {
int f_id=Integer.parseInt(f)-1;
int wh=DB.search(p);
if(f_id<5&& f_id>=0 && wh>=0){
DB.lock.lock();
DB.delete_f(DB.Pl.get(f_id), DB.PL.get(wh));
DB.delete(DB.Pl.get(f_id), DB.PL.get(wh));
DB.lock.unlock();
}
else{
//System.out.println("Invalid Flight ID");
}
}
public void my_flights(String p){
//returns the set of flights on which passenger i has a reservation
int wh=DB.search(p);
if(wh>=0){
//Lock a=new ReentrantLock();
DB.lock.lock();
for(int i=0;i<DB.PL.get(wh).Book_flight_list.size();i++){
//System.out.println(DB.PL.get(wh).Book_flight_list.get(i).id);
}
DB.lock.unlock();
}
}
public void total_reservations(){
//returns the sum total of all reservations on all flights.
DB.lock.lock();
for(int y=0;y<DB.Pl.size();y++){
//System.out.println("FLight Id : "+ DB.Pl.get(y).id);
//System.out.println("Reserved Passngers : ");
for(int h=0;h<DB.Pl.get(y).Plist.size();h++){
//System.out.println(DB.Pl.get(y).Plist.get(h).id);
}
}
DB.lock.unlock();
}
public void transfer(String f1,String f2,String p){
//transfer passenger i from flight F1 to F2. This transaction should have no impact if the passenger is not found in F1 or there is no room in F2.
int f_id1=Integer.parseInt(f1)-1;
int f_id2=Integer.parseInt(f2)-1;
int wh=DB.search(p);
if(f_id1<5&& f_id1>=0&&f_id2<5&& f_id2>=0 && wh>=0){
DB.lock.lock();
DB.replace(DB.PL.get(wh),DB.Pl.get(f_id1),DB.Pl.get(f_id2));
DB.delete(DB.Pl.get(f_id1), DB.PL.get(wh));
DB.add(DB.Pl.get(f_id2), DB.PL.get(wh));
DB.lock.unlock();
}
else{
//System.out.println("Invalid Flight ID");
}
}
@Override
public void run() {
if(this.trans_id==1)
{
this.reserve(this.flight1, this.passenger);
}
else if(this.trans_id==2)
{
this.cancel(this.flight1, this.passenger);
}
else if(this.trans_id==3)
{
this.my_flights(this.passenger);
}
else if(this.trans_id==4)
{
this.total_reservations();
}
else if(this.trans_id==5)
{
this.transfer(this.flight1, this.flight2, passenger);
}
}
}
public class SerialExecution {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
//Database DB=new Database();
Random ran = new Random();
ArrayList<TransactionsSerial> ts=new ArrayList<>();
DatabaseSerial DB=new DatabaseSerial();
int x = 50;
for(int i=0;i<x;i++){
int y = ran.nextInt(5);
//System.out.println("Transaction type: " + (1+y));
if(y==0)
{
int fight = 1 + ran.nextInt(5);
int pass= 1+ ran.nextInt(3);
//System.out.println("To reserve passenger "+pass+" in the flight "+fight);
TransactionsSerial R=new TransactionsSerial(DB,Integer.toString(fight),Integer.toString(pass), 1);
ts.add(R);
}
else if (y==1){
int fight=1+ran.nextInt(5);
int pass=1+ran.nextInt(5);
//System.out.println("To cancel passenger "+pass+" from the flight "+fight);
TransactionsSerial R=new TransactionsSerial(DB,Integer.toString(fight),Integer.toString(pass), 2);
ts.add(R);
}
else if(y==2){
int pass=1+ran.nextInt(5);
//System.out.println("Show all the flights of the passenger " + pass);
TransactionsSerial R=new TransactionsSerial(DB,Integer.toString(pass), 3);
ts.add(R);
}
else if(y==3){
TransactionsSerial R=new TransactionsSerial(DB,4);
ts.add(R);
}
else if(y==4){
int fight1 = 1 + ran.nextInt(5);
int fight2 = 1 + ran.nextInt(5);
int pass = 1 + ran.nextInt(5);
//System.out.println("To transfer passenger "+pass+" from the flight "+fight1+" to the flight "+fight2);
TransactionsSerial R=new TransactionsSerial(DB,Integer.toString(fight1),Integer.toString(fight2),Integer.toString(pass), 5);
ts.add(R);
}
}
long startTime = System.currentTimeMillis();
ExecutorService exec = Executors.newFixedThreadPool(1);
for(int i=0;i<ts.size();i++){
long startTime2 = System.currentTimeMillis();
exec.submit(ts.get(i));
long endTime2 = System.currentTimeMillis();
long totalTime2 = endTime2 - startTime2;
Thread.sleep(1);
}
if(!exec.isTerminated()) {
exec.shutdown();
exec.awaitTermination(10L,TimeUnit.SECONDS);}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
}