-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seller.java
46 lines (36 loc) · 1.2 KB
/
Seller.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
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class Seller<V> extends SellerBase<V> {
public Seller (int sleepTime, int catalogSize, Lock lock, Condition full, Condition empty, PriorityQueue<V> catalog, Queue<V> inventory) {
//TODO Complete the constructor method by initializing the attibutes
// ...
setSleepTime(sleepTime);
// this.catalogSize=catalogSize;
this.lock=lock;
this.full=full;
this.empty=empty;
this.catalog=catalog;
this.inventory=inventory;
}
@SuppressWarnings("unchecked")
public void sell() throws InterruptedException {
try {
//TODO Complete the try block for produce method
// ...
lock.lock();
while (catalog.isFull()){
full.await();
}
if (!inventory.isEmpty()){
NodeBase<V> n=inventory.dequeue();
catalog.enqueue(new Node(n.getPriority(),n));
empty.signalAll();
}
} catch(Exception e) {
e.printStackTrace();
} finally {
//TODO Complete this block
lock.unlock();
}
}
}