-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUberSeatingProblem.java
More file actions
134 lines (121 loc) · 4.32 KB
/
UberSeatingProblem.java
File metadata and controls
134 lines (121 loc) · 4.32 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
package interview;
import java.util.HashSet;import java.util.Set;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
public class UberSeatingProblem {
public static void main( String args[] ) throws InterruptedException {
UberSeatingProblem.runTest();
}
private int republicans = 0;
private int democrats = 0;
private Semaphore demsWaiting = new Semaphore(0);
private Semaphore repubsWaiting = new Semaphore(0);
CyclicBarrier barrier = new CyclicBarrier(4);
ReentrantLock lock = new ReentrantLock();
void drive() {
System.out.println("Uber Ride on Its wayyyy... with ride leader " + Thread.currentThread());
System.out.flush();
}
void seatDemocrat() throws InterruptedException, BrokenBarrierException {
boolean rideLeader = false;
lock.lock();
democrats++;
if (democrats == 4) {
// Seat all the democrats in the Uber ride.
demsWaiting.release(3);
democrats -= 4;
rideLeader = true;
} else if (democrats == 2 && republicans >= 2) {
// Seat 2 democrats & 2 republicans
demsWaiting.release(1);
repubsWaiting.release(2);
rideLeader = true;
democrats -= 2;
republicans -= 2;
} else {
lock.unlock();
demsWaiting.acquire();
}
seated();
barrier.await();
if (rideLeader == true) {
drive();
lock.unlock();
}
}
void seated() {
System.out.println(Thread.currentThread().getName() + " seated");
System.out.flush();
}
void seatRepublican() throws InterruptedException, BrokenBarrierException {
boolean rideLeader = false;
lock.lock();
republicans++;
if (republicans == 4) {
// Seat all the republicans in the Uber ride.
repubsWaiting.release(3);
rideLeader = true;
republicans -= 4;
} else if (republicans == 2 && democrats >= 2) {
// Seat 2 democrats & 2 republicans
repubsWaiting.release(1);
demsWaiting.release(2);
rideLeader = true;
republicans -= 2;
democrats -= 2;
} else {
lock.unlock();
repubsWaiting.acquire();
}
seated();
barrier.await();
if (rideLeader) {
drive();
lock.unlock();
}
}
public static void runTest() throws InterruptedException {
final UberSeatingProblem uberSeatingProblem = new UberSeatingProblem();
Set<Thread> allThreads = new HashSet<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
uberSeatingProblem.seatDemocrat();
} catch (InterruptedException ie) {
System.out.println("We have a problem");
} catch (BrokenBarrierException bbe) {
System.out.println("We have a problem");
}
}
});
thread.setName("Democrat_" + (i + 1));
allThreads.add(thread);
Thread.sleep(50);
}
for (int i = 0; i < 14; i++) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
uberSeatingProblem.seatRepublican();
} catch (InterruptedException ie) {
System.out.println("We have a problem");
} catch (BrokenBarrierException bbe) {
System.out.println("We have a problem");
}
}
});
thread.setName("Republican_" + (i + 1));
allThreads.add(thread);
Thread.sleep(20);
}
for (Thread t : allThreads) {
t.start();
}
for (Thread t : allThreads) {
t.join();
}
}
}