Browse files

fixed off-by 1 errors for some thread counts, now has a check to prev…

…ent, added some more docs
  • Loading branch information...
1 parent 3d4658f commit ec6db01c514e90a77812ecb0f583e66631272160 @anatolyg committed Feb 28, 2013
Showing with 16 additions and 6 deletions.
  1. +16 −6 src/test/WordCounter.java
View
22 src/test/WordCounter.java
@@ -18,15 +18,20 @@
* - improve data structure for storing the words:counts (two lists vs. one map)
* - make output more generic
* - use StringBuilder instead of string concatenation (Object creation)
+ * - use TreeMap for automatic sorting upon insert
*
* 3. If so, how would you change the program to make it better?
* - above
+ * - separate presentation from functionality
+ * - make it a class (not static methods)
*
* 3. How would you sort the output in the alphabetical order? In the word frequency order?
* - DONE, see below: outputCountByWords and outputCountByFrequency
*
* 4. Design the program that would calculate word frequency in multiple threads.
* - Using ThreadPoolExecutor
+ * - make the counter a Runnable
+ * - split lists into sublists, based on # threads
*
*/
public class WordCounter {
@@ -78,7 +83,7 @@ public static void countWordsSlow(List<String> text, boolean printResults) {
}
}
- static class WordFrequencyCounter extends Thread {
+ class WordFrequencyCounter implements Runnable {
List<String> text;
private boolean ignoreCase = false;
ConcurrentHashMap<String, Integer> wordCounter;
@@ -113,23 +118,28 @@ public void run() {
private ConcurrentHashMap<String, Integer> wordCounts() {
if (!this.isProcessed) {
- ThreadPoolExecutor executor = new ThreadPoolExecutor(2, this.threads, 0, TimeUnit.MILLISECONDS,
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(1, this.threads, 0, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
- int numItems = new Double(Math.floor(this.text.size()/this.threads)).intValue();
- int fromIndex = 0, toIndex = numItems;
+ int numItems = new Double(Math.ceil(this.text.size()/this.threads)).intValue();
+ int fromIndex = 0, toIndex = numItems, threadId=0;
int total = 0;
- for (int threadId=0; threadId < this.threads; threadId++) {
+ while (fromIndex != toIndex) {
System.out.println("Queuing From: " + fromIndex + ", To: " + toIndex);
List<String> words = this.text.subList(fromIndex, toIndex);
executor.execute(new WordFrequencyCounter(words, this.ignoreCase, this.wordCounter, this.globalCounter));
fromIndex = toIndex;
toIndex = numItems * (threadId+2) + 1;
+ if (toIndex > this.text.size()) {
+ toIndex = this.text.size();
+ }
total+=words.size();
+ threadId++;
}
assert(total == this.text.size());
+ assert(toIndex == this.text.size());
try {
executor.shutdown();
@@ -194,7 +204,7 @@ public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
// The utility method - is the implementation details
List<String> text = Util.readTextFromFile("monte_cristo.txt");
- WordCounter counter = new WordCounter(new ArrayList<String>(text), false, 4);
+ WordCounter counter = new WordCounter(new ArrayList<String>(text), false, 3);
System.out.println("Initial reading: " + (System.currentTimeMillis() - time) + "ms");

0 comments on commit ec6db01

Please sign in to comment.