- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment2Driver.java
More file actions
Latest commit
88 lines (71 loc) · 2.82 KB
/
Copy pathAssignment2Driver.java
File metadata and controls
88 lines (71 loc) · 2.82 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
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.concurrent.locks.Condition;
importjava.util.concurrent.locks.Lock;
importjava.util.concurrent.locks.ReentrantLock;
publicclassAssignment2Driver {
publicintcatalogSize;
publicintnumBuyers;
publicintnumSellers;
publicintsellerSleepTime, buyerSleepTime;
publicQueue<Item> inventory;
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[] args) throwsInterruptedException {
Assignment2Drivera_driver = newAssignment2Driver();
BufferedReaderreader;
intitemcount = 0;
try {
reader = newBufferedReader(newFileReader(args[0]));
Stringline = reader.readLine();
ArrayList<Node<Item>> list = newArrayList<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(" ");
Itemitem = newItem(tokens[1], Double.parseDouble(tokens[2]));
Node<Item> node = newNode<Item>(Integer.parseInt(tokens[0]), item);
list.add(node);
}
}
a_driver.inventory = newQueue<Item>(list.size());
//TODO Add all elements of the ArrayList named "list" to inventory queue
// ...
for (inti=0;i<list.size();i++)
a_driver.inventory.enqueue(list.get(i));
// ...
reader.close();
} catch (IOExceptione) {
e.printStackTrace();
}
Seller[] sellers = newSeller[a_driver.numSellers];
Buyer[] buyers = newBuyer[a_driver.numBuyers];
PriorityQueue<Node<Item>> queue = newPriorityQueue<Node<Item>>(a_driver.catalogSize);
Locklock = newReentrantLock();
Conditionfull = lock.newCondition();
Conditionempty = lock.newCondition();
intiteration = itemcount/a_driver.numBuyers; // No. of purchases each buyer make
// TODO Create multiple Buyer and Seller Threads and start them.
// ...
for (inti=0;i<a_driver.numBuyers;i++){
buyers[i]=newBuyer(a_driver.buyerSleepTime,a_driver.catalogSize,lock,full,empty,queue,iteration);
Threadb=newThread(buyers[i]);
b.start();
}
for (inti=0;i<a_driver.numSellers;i++){
sellers[i]=newSeller(a_driver.sellerSleepTime,a_driver.catalogSize,lock,full,empty,queue,a_driver.inventory);
Threads=newThread(sellers[i]);
s.start();
}
// ...
}
}