Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrent two-way map in java
    text
    copied!<p>I am writing code for text processing, and things go <strong>a lot</strong> faster if I convert the strings to integers first. To do this I made a Dictionary class, where every time I see a new string, I give it an index, and keep two maps, one from string to int, and one from int to string, so I can easily look up both ways. Here's the code:</p> <pre><code>class Dictionary { private Map&lt;String, Integer&gt; map; private Map&lt;Integer, String&gt; reverse_map; private int nextIndex; public Dictionary() { map = new HashMap&lt;String, Integer&gt;(); reverse_map = new HashMap&lt;Integer, String&gt;(); nextIndex = 1; } public int getIndex(String string) { if (!map.containsKey(string)) { map.put(string, nextIndex); reverse_map.put(nextIndex, string); nextIndex++; } return map.get(string); } public String getString(int index) { // getIndex is always called first, so we don't need to check anything return reverse_map.get(index); } } </code></pre> <p>This has been working fine for me in my single-threaded code. But now I want to give this multiple threads to speed it up more, and I'm not sure how to do it. I thought of using ConcurrentHashMap, but I'm not sure that <code>putIfAbsent</code> will guarantee that I don't use an index twice. I didn't want to use Collections.synchronizedMap, because this dictionary is accessed really frequently across the threads and so I probably wouldn't be much better off than with a single thread, because it blocks on every read and write. Is there a way to make this work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload