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 7066743
In Process

The Archive Base Latest Questions

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

I have a complex map, which doesn’t need locking for the frequent gets and

  • 0

I have a complex map, which doesn’t need locking for the frequent gets and infrequent changes, because the keys uniquely determine the behavior of their referenced elements.

That is, if the key is the same, the structure it references will always have the same behavior. In fact, the key is immutable final and the referenced value is effectively final and/or threadsafe.

Lets say I have a volatile reference to the underlying volatile array of references in my custom hashmap. My hashmap algorithm for this array uses the ar.length member as the modulus so if in the get function, the caller sees a non-current array, all of its references will still be good, and still obey the hashing slots, so if it succeeds without mutex to get a non-null value, that’s the right one.

My plan is whenever a get fails, the caller then constructs the correct value for the key does a put which does locking relative to other puts and stuffs an object into the array. Just before exiting the critical section the put code reassigns the volatile array field “ar” to itself, hopefully as a message to the compiler and hotspot compiler to create a fence relative to the gets which use the volatile array reference to find a hashed value.

This will work as long as the compiler doesn’t nop the “ar = ar” assignment:

private volatile Object[] ar;

public Object get (Object key)
{
    Object[] ar = this.ar;
    // get the value from the correct hash slot
}

public synchronized Object put (Object key, Object val) {
{
    ... stuff a new object into the correct hash slot

    ar = ar;             // will the compiler truly compile this statement with 
                         // normal volatile fencing relative to the get function??

    ... if a race condition causes a value to already be mapped, return and use that
    ... rather than the one we created and passed to put (different than normal put
    ... contract)
}
  • 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-28T05:07:03+00:00Added an answer on May 28, 2026 at 5:07 am

    A volatile write can’t be optimized away, so yes you’ll get the memory guarantees here. And since reading a value from an array also (at least conceptually) means that you read the volatile variable of the array you should get one guaranteed volatile read either.

    So while this should work – if you’re using Hotspot the usual way to do this is with sun.misc.Unsafe – you can look at the concurrent collections in Java5 upwards where that pattern is demonstrated often enough. (And yes we can all look forward to getting arrays of volatile elements in the future – afaik Doug Lea and Co are working on a specification for that, but no idea how far they are.)

    Although the question is why you implement this yourself – there’s Cliff’s non blocking hashmap that has some pretty strong correctness guarantees (afaik they checked it with CHESS for one and lots of people have looked at the underlying state machine) and excellent performance compared to the ConcurrentHashMap from the JDK.

    And certainly faster than having a synchronized put operation.

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

Sidebar

Related Questions

I have to create a pretty complex map which is divided in pieces, which
I have several complex data structures like Map< A, Set< B > > Set<
I am using WCF and REST, and I have complex types, which are working
I have a complex WPF UserControl made of other ContentControl templates which contain sets
I have a Map activity in a fairly complex layout. As such, I can't
I have a svg file which contains complex paths with bezier curves in it.
Problem I have timestamped data, which I need to search based on the timestamp
I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex
I have a custom implementation of the Map interface which does some fancy stuff,
We have a pretty complex BizTalk interface which uses the HL7 accelerator. We use

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.