-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment2Driver.java
88 lines (71 loc) · 2.82 KB
/
Assignment2Driver.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
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Assignment2Driver {
public int catalogSize;
public int numBuyers;
public int numSellers;
public int sellerSleepTime, buyerSleepTime;
public Queue<Item> inventory;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InterruptedException {
Assignment2Driver a_driver = new Assignment2Driver();
BufferedReader reader;
int itemcount = 0;
try {
reader = new BufferedReader(new FileReader(args[0]));
String line = reader.readLine();
ArrayList<Node<Item>> list = new ArrayList<Node<Item>>();
String[] tokens = line.split(" ");
a_driver.catalogSize = Integer.parseInt(tokens[0]);
a_driver.numBuyers = Integer.parseInt(tokens[1]);
a_driver.numSellers = Integer.parseInt(tokens[2]);
a_driver.buyerSleepTime = Integer.parseInt(tokens[3]);
a_driver.sellerSleepTime = Integer.parseInt(tokens[4]);
while (line != null) {
line = reader.readLine();
if (line != null) {
itemcount++;
tokens = line.split(" ");
Item item = new Item(tokens[1], Double.parseDouble(tokens[2]));
Node<Item> node = new Node<Item>(Integer.parseInt(tokens[0]), item);
list.add(node);
}
}
a_driver.inventory = new Queue<Item>(list.size());
//TODO Add all elements of the ArrayList named "list" to inventory queue
// ...
for (int i=0;i<list.size();i++)
a_driver.inventory.enqueue(list.get(i));
// ...
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Seller[] sellers = new Seller[a_driver.numSellers];
Buyer[] buyers = new Buyer[a_driver.numBuyers];
PriorityQueue<Node<Item>> queue = new PriorityQueue<Node<Item>>(a_driver.catalogSize);
Lock lock = new ReentrantLock();
Condition full = lock.newCondition();
Condition empty = lock.newCondition();
int iteration = itemcount/a_driver.numBuyers; // No. of purchases each buyer make
// TODO Create multiple Buyer and Seller Threads and start them.
// ...
for (int i=0;i<a_driver.numBuyers;i++){
buyers[i]=new Buyer(a_driver.buyerSleepTime,a_driver.catalogSize,lock,full,empty,queue,iteration);
Thread b=new Thread(buyers[i]);
b.start();
}
for (int i=0;i<a_driver.numSellers;i++){
sellers[i]=new Seller(a_driver.sellerSleepTime,a_driver.catalogSize,lock,full,empty,queue,a_driver.inventory);
Thread s=new Thread(sellers[i]);
s.start();
}
// ...
}
}