Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are on the right track using <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html" rel="noreferrer"><code>ConcurrentHashMap</code></a>. For each point:</p> <ol> <li>Check out the methods <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent(K%2C%20V)" rel="noreferrer"><code>putIfAbsent</code></a> and <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#replace(K%2C%20V)" rel="noreferrer"><code>replace</code></a> both are threadsafe and combine checking current state of hashmap and updating it into one atomic operation.</li> <li>The <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#get(java.lang.Object)" rel="noreferrer">get</a> method is not synchronized internally but will return the most recent value for the specified key available to it (check the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html" rel="noreferrer">ConcurrentHashMap class Javadoc for discussion</a>). </li> </ol> <p>The benefit of <code>ConcurrentHashMap</code> over something like <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map)" rel="noreferrer"><code>Collections.synchronizedMap</code></a> is the combined methods like <code>putIfAbsent</code> which provide traditional Map <code>get</code> and <code>put</code> logic in an internally synchronized way. Use these methods and <em>do not</em> try to provide your own custom synchronization over <code>ConcurrentHashMap</code> as it will not work. The <code>java.util.concurrent</code> collections are internally synchronized and other threads will not respond to attempts at synchronizing the object (e.g. <code>synchronize(myConcurrentHashMap){}</code> will not block other threads).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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