-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.java
More file actions
208 lines (163 loc) · 5.49 KB
/
Route.java
File metadata and controls
208 lines (163 loc) · 5.49 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
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
package ticketingsystem;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Route {
private ExecutorService executorService;
private AtomicInteger atomicInteger = new AtomicInteger(0);
private int routeid;
private int coachnum;
private int seatnum;
private int stationnum;
private List<Set<Integer>> idleSeat = new ArrayList<Set<Integer>>();
private List<ReentrantReadWriteLock> lockList = new ArrayList<ReentrantReadWriteLock>();
public Route(ExecutorService executorService, int routeid, int coachnum, int seatnum, int stationnum){
this.executorService = executorService;
this.routeid = routeid;
this.coachnum = coachnum;
this.seatnum = seatnum;
this.stationnum = stationnum;
for(int i = 0; i < stationnum; i++){
Set<Integer> seats = new HashSet<Integer>();
for(int j = 0; j < coachnum * seatnum; j++){
seats.add(j);
}
idleSeat.add(seats);
lockList.add(new ReentrantReadWriteLock());
}
}
public Ticket buyTicket(String passengers, int route, int departure, int arrival) throws InterruptedException, ExecutionException{
FutureTask<Ticket> futureTask = new FutureTask<Ticket>(new GetSeatTask(passengers, departure, arrival));
executorService.submit(futureTask);
return futureTask.get();
}
public int inquiry(int route, int departure, int arrival) throws InterruptedException, ExecutionException{
FutureTask<Integer> futureTask = new FutureTask<Integer>(new QuerySeatTask(departure, arrival));
executorService.submit(futureTask);
return futureTask.get();
}
public boolean refundTicket(Ticket ticket) throws InterruptedException, ExecutionException{
FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new RefundTask(ticket));
executorService.submit(futureTask);
return futureTask.get();
}
private class GetSeatTask implements Callable<Ticket> {
private String passenger;
private int from;
private int to;
public GetSeatTask(String passenger, int departure, int arrival){
this.passenger = passenger;
this.from = departure - 1;
this.to = arrival - 1;
}
@Override
public Ticket call() throws Exception {
try {
for(int i = from; i <= to; i++){
lockList.get(i).writeLock().lock();
}
Set<Integer> tmpSeats = new HashSet<Integer>();
tmpSeats.addAll(idleSeat.get(from));
for(int i = from + 1; i <= to; i++){
tmpSeats.retainAll(idleSeat.get(i));
}
if(tmpSeats.size() == 0){
return null;
}
long mod = (long)(1e10);
long threadid = Thread.currentThread().getId() % mod;
long tid = (System.currentTimeMillis() * mod + threadid) * 10000 + atomicInteger.incrementAndGet();
int seat = tmpSeats.iterator().next();
int[] tic = toSeat(seat);
for(int i = from; i <= to; i++){
idleSeat.get(i).remove(seat);
}
return new Ticket(Math.abs(tid), passenger, routeid, tic[0], tic[1], from + 1, to + 1);
} finally {
for(int i = from; i <= to; i++){
lockList.get(i).writeLock().unlock();
}
}
}
}
private class QuerySeatTask implements Callable<Integer>{
private int from;
private int to;
public QuerySeatTask(int departure, int arrival) {
this.from = departure - 1;
this.to = arrival - 1;
}
@Override
public Integer call() throws Exception {
try {
for(int i = from; i <= to; i++){
lockList.get(i).readLock().lock();
}
Set<Integer> tmpSeats = new HashSet<Integer>();
tmpSeats.addAll(idleSeat.get(from));
for(int i = from + 1; i <= to; i++){
tmpSeats.retainAll(idleSeat.get(i));
}
return tmpSeats.size();
} finally {
for(int i = from; i <= to; i++){
lockList.get(i).readLock().unlock();
}
}
}
}
public class RefundTask implements Callable<Boolean>{
private Ticket ticket;
private int from;
private int to;
public RefundTask(Ticket ticket){
this.ticket = ticket;
this.from = ticket.getDeparture() - 1;
this.to = ticket.getArrival() - 1;
}
@Override
public Boolean call() throws Exception {
if(from < 0 || from >= stationnum ||
to < 0 || to >= stationnum ||
from >= to ||
ticket.getCoach() <= 0 || ticket.getCoach() > coachnum ||
ticket.getSeat() <= 0 || ticket.getSeat() > seatnum){
return false;
}
try {
for(int i = from; i <= to; i++){
lockList.get(i).writeLock().lock();
}
Integer seat = toNum(ticket.getCoach(), ticket.getSeat());
for(int i = from; i <= to; i++){
if(idleSeat.get(i).contains(seat)){
return false;
}
}
for(int i = from; i <= to; i++){
idleSeat.get(i).add(seat);
}
return true;
} finally {
for(int i = from; i <= to; i++){
lockList.get(i).writeLock().unlock();
}
}
}
}
private int[] toSeat(int num){
int coaNum = num / seatnum + 1;
int seNum = num % seatnum + 1;
return new int[]{coaNum, seNum};
}
private Integer toNum(int coaNum, int seNum){
return (coaNum - 1) * seatnum + seNum - 1;
}
}