-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyer.java
More file actions
43 lines (35 loc) · 1.17 KB
/
Buyer.java
File metadata and controls
43 lines (35 loc) · 1.17 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
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class Buyer<V> extends BuyerBase<V> {
public Buyer (int sleepTime, int catalogSize, Lock lock, Condition full, Condition empty, PriorityQueue<V> catalog, int iteration) {
//TODO Complete the Buyer Constructor method
// ...
setSleepTime(sleepTime);
// this.catalogSize=catalogSize;
this.lock=lock;
this.full=full;
this.empty=empty;
this.catalog=catalog;
setIteration(iteration);
}
public void buy() throws InterruptedException {
try {
//TODO Complete the try block for consume method
// ...
lock.lock();
while (catalog.isEmpty()){
empty.await();
}
Node<V> n = (Node<V>) catalog.dequeue();
System.out.print("Consumed "); // DO NOT REMOVE (For Automated Testing)
n.show(); // DO NOT REMOVE (For Automated Testing)
full.signalAll();
// ...
} catch (Exception e) {
e.printStackTrace();
} finally {
//TODO Complete this block
lock.unlock();
}
}
}