-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBases.java
More file actions
80 lines (68 loc) · 2.83 KB
/
Bases.java
File metadata and controls
80 lines (68 loc) · 2.83 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
// WARNING: DO NOT CHANGE THIS FILE
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
// Base class for Node. Each node would be an element of the Priority Queue.
abstract class NodeBase<V> {
protected int priority; // Priority of the node
protected V value; // Value stored by the node
public abstract int getPriority(); // return the priority of the node
public abstract V getValue(); // returns the value stored by the node
public void show() {
System.out.println(this.getPriority());
}
}
// Interface for Queue
interface QueueInterface<V> {
public int size(); // Returns the size, i.e. the number of elements in the queue
public boolean isEmpty(); // Returns true if the queue is empty, else returns false
public boolean isFull(); // Returns true if the queue is full, else returns false
public void enqueue(Node<V> item); // Adds an item to the rear of the queue
public NodeBase<V> dequeue(); // Removes an item from the front of the queue
}
// Base class for Buyer
abstract class BuyerBase<V> implements Runnable {
protected PriorityQueue<V> catalog; // The shared priority queue
protected Lock lock; // Shared lock
protected Condition full, empty; // Shared condition variables
private int sleepTime; // Sleep duration (in ms) for current thread
private int iteration; // No. of iterations for buyer threads
public abstract void buy() throws InterruptedException; // Implements the buy (consume) method from shared queue
// Implements the thread run method
public void run() {
for(int i=0;i<this.iteration;i++) {
try {
buy();
Thread.sleep(this.sleepTime);
} catch (Exception e) { e.printStackTrace(); }
}
}
// Setter for sleepTime
public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}
// Setter for iteration
public void setIteration(int iteration) {
this.iteration = iteration;
}
}
// Base class for Seller
abstract class SellerBase<V> implements Runnable {
protected PriorityQueue<V> catalog; // Shared priority queue
protected Lock lock; //Shared lock
protected Condition full, empty; // Shared condition variables
private int sleepTime; // Sleep duration (in ms) for current thread
protected Queue<V> inventory; // List of items (shared between sellers)
public abstract void sell() throws InterruptedException; // Implements the sell (produce) method to shared queue
// Implements the thread run method
public void run() {
while(inventory.isEmpty()!=true) {
try {
sell();
} catch (Exception e) { e.printStackTrace(); }
}
}
// Setter for sleepTime
public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}
}