-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrace.java
More file actions
116 lines (97 loc) · 5.24 KB
/
Trace.java
File metadata and controls
116 lines (97 loc) · 5.24 KB
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
package ticketingsystem;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
class ThreadId {
// Atomic integer containing the next thread ID to be assigned
private static final AtomicInteger nextId = new AtomicInteger(0);
// Thread local variable containing each thread's ID
private static final ThreadLocal<Integer> threadId =
new ThreadLocal<Integer>() {
@Override protected Integer initialValue() {
return nextId.getAndIncrement();
}
};
// Returns the current thread's unique ID, assigning it if necessary
public static int get() {
return threadId.get();
}
}
public class Trace {
final static int threadnum = 4;
final static int routenum = 3; // route is designed from 1 to 3
final static int coachnum = 5; // coach is arranged from 1 to 5
final static int seatnum = 10; // seat is allocated from 1 to 20
final static int stationnum = 8; // station is designed from 1 to 5
final static int testnum = 1000;
final static int retpc = 30; // return ticket operation is 10% percent
final static int buypc = 60; // buy ticket operation is 30% percent
final static int inqpc = 100; //inquiry ticket operation is 60% percent
static String passengerName() {
Random rand = new Random();
long uid = rand.nextInt(testnum);
return "passenger" + uid;
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[threadnum];
final TicketingDS tds = new TicketingDS(routenum, coachnum, seatnum, stationnum, threadnum);
final long startTime = System.nanoTime();
for (int i = 0; i< threadnum; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
Random rand = new Random();
Ticket ticket = new Ticket();
ArrayList<Ticket> soldTicket = new ArrayList<Ticket>();
for (int i = 0; i < testnum; i++) {
int sel = rand.nextInt(inqpc);
if (0 <= sel && sel < retpc && soldTicket.size() > 0) { // return ticket
int select = rand.nextInt(soldTicket.size());
if ((ticket = soldTicket.remove(select)) != null) {
long preTime = System.nanoTime() - startTime;
if (tds.refundTicket(ticket)) {
long postTime = System.nanoTime() - startTime;
System.out.println(preTime + " " + postTime + " " + ThreadId.get() + " " + "TicketRefund" + " " + ticket.tid + " " + ticket.passenger + " " + ticket.route + " " + ticket.coach + " " + ticket.departure + " " + ticket.arrival + " " + ticket.seat);
System.out.flush();
} else {
System.out.println(preTime + " " + String.valueOf(System.nanoTime()-startTime) + " " + ThreadId.get() + " " + "ErrOfRefund");
System.out.flush();
}
} else {
long preTime = System.nanoTime() - startTime;
System.out.println(preTime + " " + String.valueOf(System.nanoTime()-startTime) + " " + ThreadId.get() + " " + "ErrOfRefund");
System.out.flush();
}
} else if (retpc <= sel && sel < buypc) { // buy ticket
String passenger = passengerName();
int route = rand.nextInt(routenum) + 1;
int departure = rand.nextInt(stationnum - 1) + 1;
int arrival = departure + rand.nextInt(stationnum - departure) + 1; // arrival is always greater than departure
long preTime = System.nanoTime() - startTime;
if ((ticket = tds.buyTicket(passenger, route, departure, arrival)) != null) {
long postTime = System.nanoTime() - startTime;
System.out.println(preTime + " " + postTime + " " + ThreadId.get() + " " + "TicketBought" + " " + ticket.tid + " " + ticket.passenger + " " + ticket.route + " " + ticket.coach + " " + ticket.departure + " " + ticket.arrival + " " + ticket.seat);
soldTicket.add(ticket);
System.out.flush();
} else {
System.out.println(preTime + " " + String.valueOf(System.nanoTime()-startTime) + " " + ThreadId.get() + " " + "TicketSoldOut" + " " + route + " " + departure+ " " + arrival);
System.out.flush();
}
} else if (buypc <= sel && sel < inqpc) { // inquiry ticket
int route = rand.nextInt(routenum) + 1;
int departure = rand.nextInt(stationnum - 1) + 1;
int arrival = departure + rand.nextInt(stationnum - departure) + 1; // arrival is always greater than departure
long preTime = System.nanoTime() - startTime;
int leftTicket = tds.inquiry(route, departure, arrival);
long postTime = System.nanoTime() - startTime;
System.out.println(preTime + " " + postTime + " " + ThreadId.get() + " " + "RemainTicket" + " " + leftTicket + " " + route+ " " + departure+ " " + arrival);
System.out.flush();
}
}
}
});
threads[i].start();
}
for (int i = 0; i< threadnum; i++) {
threads[i].join();
}
}
}