diff --git a/source/de/anomic/tools/DidYouMean.java b/source/de/anomic/tools/DidYouMean.java index 9ca8e3518..72dff5a8c 100644 --- a/source/de/anomic/tools/DidYouMean.java +++ b/source/de/anomic/tools/DidYouMean.java @@ -2,8 +2,8 @@ package de.anomic.tools; import java.util.Collections; import java.util.HashSet; -import java.util.LinkedList; import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; import de.anomic.kelondro.text.IndexCell; import de.anomic.kelondro.text.referencePrototype.WordReference; @@ -23,69 +23,66 @@ public class DidYouMean { 'q','r','s','t','u','v','w','x','y','z','\u00e4','\u00f6','\u00fc','\u00df'}; private static final long TIMEOUT = 500; + final LinkedBlockingQueue queue = new LinkedBlockingQueue(); + private final Set set; private final IndexCell index; private String word; private int len; - private Thread ChangingOneLetter; - private Thread AddingOneLetter; - private Thread DeletingOneLetter; - private Thread ReversingTwoConsecutiveLetters; - - private final BlockingQueue bq = new BlockingQueue(); - public DidYouMean(final IndexCell index) { // this.set = Collections.synchronizedSortedSet(new TreeSet(new wordSizeComparator())); this.set = Collections.synchronizedSet(new HashSet()); this.word = ""; this.len = 0; this.index = index; - - this.ChangingOneLetter = new ChangingOneLetter(); - this.AddingOneLetter = new AddingOneLetter(); - this.DeletingOneLetter = new DeletingOneLetter(); - this.ReversingTwoConsecutiveLetters = new ReversingTwoConsecutiveLetters(); } public Set getSuggestion(final String word) { long startTime = System.currentTimeMillis(); this.word = word.toLowerCase(); this.len = word.length(); - - // create worker threads - Thread[] workers = new Thread[8]; - for (int i=0; i 0) { - run = true; - } else { - run = false; - } + // create producers + Thread[] producers = new Thread[4]; + producers[0] = new ChangingOneLetter(); + producers[1] = new AddingOneLetter(); + producers[2] = new DeletingOneLetter(); + producers[3] = new ReversingTwoConsecutiveLetters(); + + // start producers + for (int i=0; i0) { + // consume more ... } - // push "poison pill" for each worker thread - for (int i=0; i0?"(timed out)":"")); return this.set; @@ -93,17 +90,17 @@ public class DidYouMean { private class ChangingOneLetter extends Thread { - public ChangingOneLetter() { - this.setName("ChangingOneLetter"); - } - // tests: alphabet.length * len public void run() { String s; for(int i=0; i queue = new LinkedList(); - - public void push(Thread t) { - synchronized(queue) { - queue.add(t); - queue.notify(); + } catch (InterruptedException e) { + return; } } - public Object pop() throws InterruptedException { - synchronized(queue) { - while (queue.isEmpty()) { - queue.wait(); - } - return queue.removeFirst(); + void consume(String s) { + if (index.has(Word.word2hash(s))) { + set.add(s); } } - public int size() { - return queue.size(); - } - } + } /* private class wordSizeComparator implements Comparator {