2017-02-16 56 views
1

所以ArrayList「comb」包含長度相等的字符串和某些字符的變體。在最壞的情況下,這個列表可以包含大約100,000個字。函數checkWord(String str)將一個單詞作爲參數,並檢查該單詞是否存在於Hashtable字典中(其中包含另外90,000個字,文本文件已讀入此散列表)。所以基本上,代碼需要檢查List「comb」中的哪些單詞出現在HashTable「dictionary」中。在最壞的情況下,這個搜索需要花費5分鐘。我想實現Runnable並將其並行化,但不確定如何去做。如何正確實現Runnable在Hashtable中搜索元素?

例如:列表梳包含CURMUDGEON的各種拼寫錯誤和正確的單詞本身。該列表包含98415個。 CURMUEGEON CURMUEGEOH CURMUEGEOJ CURMUEGEKN等等。因此,檢查這些單詞是否存在於哈希表中需要200秒。我想打倒這個時候

class key implements Runnable{ 
    public static ArrayList<String> comb; 
    public static Hashtable<String,String> dictionary; 
    public static void main(String[] args) throws IOException{ 
     key obj = new key(); 
     Thread thread1 = new Thread(obj); 
     thread1.start(); 
    } 
    public static Boolean checkWord(String str){ 
       String toCheck = str.toLowerCase(); 
       if(dictionary.contains(toCheck)){ 
        return true; 
       } 
       else 
       return false; 
     } 
     public void run(){ 
      for(String x:comb) 
       if (checkWord(x)) 
        filtered.add(x); 

     } 
+1

在HashMap中查找100,000個單詞應該是秒的順序,如果是這樣的話。做多線程是沒有意義的。你確定'dictionary'確實是一個基於散列表的數據結構嗎?請提供[mcve]。 –

+0

@JonSkeet謝謝你,我編輯和更新了我的問題。 – daipayan

+2

嗯...... 1)爲什麼這是一個'Map'而不是'Set'?什麼是Map的值? 2)並行化的代價是複雜性。如果這些值是不相關的,我們對[set intersection]有更好的算法(http://stackoverflow.com/questions/4642172/computing-set-intersection-in-linear-time)。 – dhke

回答

1

HashTable是一個傳統的JDK1.0 API類,具有非常強大的併發保證。在particular,

與新的集合實現不同,Hashtable是同步的。

這意味着Hashtable上的每個操作都需要獲得監視器鎖定,這是反覆查找的性能殺手。最好遵循JDK javadoc中給出的建議:

如果不需要線程安全的實現,建議使用HashMap代替Hashtable。如果需要線程安全的高度並行實現,則建議使用ConcurrentHashMap代替Hashtable。

0

爲了使這個效率,您需要一個獨立於梳名單的測試不同的範圍,像

public class MySearcher implements Runnable { 
    ArrayList list; 
    int startIdx, endIdx; 
    public MySearcher(list, startIdx, endIdx) { 
    // copy into object fields 
    } 
    public void run() { 
    // test all values in the list between startIdx and endIdx 
    // put results into a data structure. Create a method to get/return that data structure 
    } 
} 

多的Runnable然後你可以使用一個ExecutorService所有您的Runnables(有關用法,請參閱javadoc:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html