开发者

Thread safe singleton [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Thread safety in Singleton

hi,

I am using wordnet dictionary to find the synonyms for words. As I have lots documents I have used multiple threads to do the document pre-processing which involves stemming, stop word removal and synonym replacement.

I am using the following code to access the dictionary and get the wordset for each document.

IndexWordSet set = Dictionary.getInstance().lookupAllIndexWords(newWord);

This works fine in a single threaded environment. But in a multi threaded environment this didn't work as expected. The program gets stuck after some time.

Is this because the Dictionary.getInstance() is a singleton class and it is not thread safe? If so how can I modify the access to the dictionary so that it is thread safe? ( I cannot modify the dictionary class since I have use开发者_JS百科d a dictionary library)


Write a wrapper for your Dictationary instance. In this wrapper synchronize the access to ensure that only one thread can access to lookupAllIndexWords() at a time.

public class DictionaryIndexer {
   public static IndexWordSet lookupAllIndexWords(newWord) {
       final Dictionary instance = Dictionary.getInstance();
       synchronized (instance) {
           return instance.lookupAllIndexWords(newWord);
       }
   }
}

If you encapsulate all the calls to Dictionary with your wrapper using the same lock for synchronization you migth be able to have a thread safe solution.


From the source:

You have iterators and state all over this library:

/**
 * Main word lookup procedure. First try a normal lookup. If that doesn't work,
 * try looking up the stemmed form of the lemma.
 * @param pos the part-of-speech of the word to look up
 * @param lemma the lemma to look up
 * @return IndexWord the IndexWord found by the lookup procedure, or null
 *              if an IndexWord is not found
 */
public IndexWord lookupIndexWord(POS pos, String lemma) throws JWNLException {
    lemma = prepareQueryString(lemma);
    IndexWord word = getIndexWord(pos, lemma);
    if (word == null && getMorphologicalProcessor() != null) {
        word = getMorphologicalProcessor().lookupBaseForm(pos, lemma);
    }
    return word;
}



/**
 * Return a set of <code>IndexWord</code>s, with each element in the set
 * corresponding to a part-of-speech of <var>word</var>.
 * @param lemma the word for which to lookup senses
 * @return An array of IndexWords, each of which is a sense of <var>word</var>
 */
public IndexWordSet lookupAllIndexWords(String lemma) throws JWNLException {
    lemma = prepareQueryString(lemma);
    IndexWordSet set = new IndexWordSet(lemma);
    for (Iterator itr = POS.getAllPOS().iterator(); itr.hasNext();) {
        IndexWord current = lookupIndexWord((POS)itr.next(), lemma);
        if (current != null) set.add(current);
    }
    return set;
}

and in POS we find

private static final List ALL_POS =
    Collections.unmodifiableList(  /* alphazero: this is good news .. */
            Arrays.asList(new POS[] {NOUN, VERB, ADJECTIVE, ADVERB}));

public static List getAllPOS() {
    return ALL_POS;
}

Try Lynch's answer. It should work.


You can use one of the concurrent containers...

Alternately you can use synchronization within the singleton instance (somebody already posted a link for the thread safe singleton in the comments).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜