Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:03:19+00:00 2026-05-28T06:03:19+00:00

I need to write a somewhat specific implementation of a cache, which has unique

  • 0

I need to write a somewhat specific implementation of a cache, which has unique keys but can contain duplicate values, e.g.:

 "/path/to/one" -> 1
 "/path/to/two" -> 2
 "/path/to/vienas" -> 1
 "/path/to/du" -> 2

The class needs to offer non blocking reads/key lookups but also has typical create/update/delete mutators. For example, removing value 2 should result

"/path/to/one" -> 1
"/path/to/vienas" -> 1

Reads for this cache will outweigh the writes by far so write performance is not an issue – as long as concurrent writes do not run on top of each other. The overall number of entries is much likely going to be less than 1000 so occasional iterating over values is still affordable.

So I wrote something like this (pseudo code):

//
// tl;dr all writes are synchronized on a single lock and each
// resets the reference to the volatile immutable map after finishing
//
class CopyOnWriteCache {
   private volatile Map<K, V> readOnlyMap = ImmutableMap.of();

   private final Object writeLock = new Object();

   public void add(CacheEntry entry) {
      synchronized (writeLock) {
         readOnlyMap = new ImmutableMap.Builder<K, V>()
            .addAll(readOnlyMap)
            .add(entry.key, entry.value)
            .build();
      }
   }

   public void remove(CacheEntry entry) {
      synchronized (writeLock) {
         Map<K, V> filtered = Maps.filterValues(readOnlyMap, somePredicate(entry));
         readOnlyMap = ImmutableMap.copyOf(filtered);
      }
   }

   public void update(CacheEntry entry) {
      synchronized (writeLock) {
         Map<K, V> filtered = Maps.filterValues(readOnlyMap, somePredicate(entry));
         readOnlyMap = new ImmutableMap.Builder<K, V>()
             .addAll(filtered)
             .add(entry.key, entry.value)
             .build();
      }
   }

   public SomeValue lookup(K key) {
      return readOnlyMap.get(key);
   }
}

After writing the above, I realized that ConcurrentHashMap also offers non-blocking reads which would make all my effort pointless, but there’s a statement in its Javadoc which raises an eyebrow:

iterators are designed to be used by only one thread at a time

So if I replace the usage of volatile ImmutableMap with final ConcurrentHashMap and remove all synchronized blocks, is it possible that competing concurrent mutators will invalidate each other? For example, I can imagine how two concurrent calls to remove would lead to a race condition, completely invalidating the results of the first remove.

The only improvement I can see is that by using final ConcurrentHashMap and leaving synchronized as they are I could at least avoid unnecessary copying of data.

Does that make sense – or maybe I’m overlooking something here? Can anyone suggest other alternatives for this solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:03:19+00:00Added an answer on May 28, 2026 at 6:03 am

    If you made this replacement, you would still have only one thread using a given Iterator at once.
    The warning means that two threads should not use the same Iterator instance. Not that two threads can’t iterate concurrently.

    The problem you would have is that since the removal operation can’t be done in a single atomic operation of the ConcurrentMap, you could have a concurrent thread see the map in an intermediate state: one value has been removed but not the other one.

    I’m not sure this would be any faster, since you say that write performance is not an issue, but what you could do to avoid the copy of the map at each write, is to use a ReadWriteLock guarding a mutable ConcurrentMap. All the reads would still be concurrent, but a write to the map would prevent all the other threads to access the map. And you wouldn’t have to create a fresh copy each time it’s modified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a customer, I need to write a Servlet which can print values in
I need write an update statement that used multiple tables to determine which rows
I need to write a rule to redirect any image file to a specific
I'm somewhat informed with TDD and BDD with Ruby/Rails, but I will eventually need
OS X lacks linux's strace , but it has dtrace which is supposed to
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one
I need to write a Delphi application that pulls entries up from various tables
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write a java script. This is supposed to validate if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.