4 comments

  • mrkeen38 days ago
    <p><pre><code> public V get(Object key) { synchronized (mutex) { ... } } public V put(K key, V value) { synchronized (mutex) { ... } } public V remove(Object key) { synchronized (mutex) { ... } } </code></pre> &gt; From a correctness standpoint, this strategy is almost trivial to reason about.<p>It is indeed trivial to reason about. This design invites users to stumble into the race condition between get and set.
    • PaulHoule38 days ago
      What a good transactional API is like is an interesting question.<p><pre><code> public void modify(K key, Function&lt;Optional&lt;V&gt;,Optional&lt;V&gt;&gt; modifier) </code></pre> or something along those lines covers the race of a single value being changed out from under you, but if you want to update one or more keys you might want<p><pre><code> public void modifyMany(Set&lt;K&gt; key, Consumer&lt;Map&lt;K,V&gt;&gt; modifier) </code></pre> where the modifier gets passed a modifiable view that has just the keys in the set.<p>There is also the Clojure-style immutable API for maps though I can say I never really enjoyed working with that the way I enjoyed immutable lists (which are Lispier than Lisp: I went through all the stages of grief reading <i>On Lisp</i> and couldn&#x27;t help think &quot;If he was using Clojure he wouldn&#x27;t be struggling with <i>nconc</i>)
      • mrkeen35 days ago
        STM containers does it right.<p><pre><code> lookup :: k -&gt; Map K v -&gt; STM (Maybe v) insert :: v -&gt; k -&gt; Map K v -&gt; STM () </code></pre> It won&#x27;t execute until you wrap it with atomically, so you&#x27;re forced to be explicit about whether these things should happen in one logical step or separately.<p><a href="https:&#x2F;&#x2F;hackage-content.haskell.org&#x2F;package&#x2F;stm-containers&#x2F;docs&#x2F;StmContainers-Map.html" rel="nofollow">https:&#x2F;&#x2F;hackage-content.haskell.org&#x2F;package&#x2F;stm-containers&#x2F;d...</a>
    • eterm38 days ago
      There&#x27;s a design in the .NET ConcurrentDictionary that is even more inviting:<p><pre><code> GetOrAdd(TKey, Func&lt;Tkey,TValue&gt; ) </code></pre> This lets you specify a key, and a method to run to generate a value to store if the key does not exist.<p>This is thread-safe, however it is <i>not atomic</i> much to the surprise of many that use it.<p>If you wrongly assume that the factory method can only execute once, you fall into a trap, since it actually can execute many times before the result is stored.<p>This can cause a problem if you use it for caching, because you might for example put an expensive calculation you want to cache there, only to find out cache expiration causes a stampede as the factory suddenly executes a lot at once.
    • the-smug-one37 days ago
      I don&#x27;t see the problem at all, tbh. Both `get(K); put(K);` and `put(K); get(K);` are valid execution traces on a uniprocessor.
      • mrkeen35 days ago
        Oh yeah, I&#x27;m not sure what an execution trace is, but ConcurrentHashMap is indeed fit-for-purpose for single-threaded use.
  • nickmonad38 days ago
    Would love to read this, although I&#x27;m seeing some pretty horrific code formatting issues in both Firefox and Chrome.
    • ramon15638 days ago
      weirdest part is, the initial load looks fine (try refreshing, scroll persists).<p>firefox&#x27;s reader view helps too
      • manwe15038 days ago
        I saw a blog about this yesterday: disable extensions (notably 1Password) to fix formatting inside code tags.
    • pama38 days ago
      Looks decent on iPhone safari FWIW.
    • NooneAtAll338 days ago
      turn off 1password
      • saghm38 days ago
        More context for those who haven&#x27;t heard about this: <a href="https:&#x2F;&#x2F;www.1password.community&#x2F;discussions&#x2F;developers&#x2F;1password-chrome-extension-is-incorrectly-manipulating--blocks&#x2F;165639" rel="nofollow">https:&#x2F;&#x2F;www.1password.community&#x2F;discussions&#x2F;developers&#x2F;1pass...</a>
  • rurban36 days ago
    ConcurrentSwissMap exists for years already: <a href="https:&#x2F;&#x2F;github.com&#x2F;greg7mdp&#x2F;parallel-hashmap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;greg7mdp&#x2F;parallel-hashmap</a> Just in C++, not Java.
  • leitasat38 days ago
    &gt; 2025 &gt; Java<p>really