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

  • SEARCH
  • Home
  • 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 8167735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:17:34+00:00 2026-06-06T20:17:34+00:00

I want to pass a pre-built Map to multiple threads that are going to

  • 0

I want to pass a pre-built Map to multiple threads that are going to only read the values from the Map simultaneously. Do I need to worry about concurrent access and use the ConcurrentHashMap instead of HashMap in this case?

  • 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-06-06T20:17:36+00:00Added an answer on June 6, 2026 at 8:17 pm

    You still need to make sure that there is a “happens-before” relationship between the thread that creates and initializes the Map, and the threads that subsequently read it. If there is no such relationship, it is theoretically possible for the reading threads to see stale data.

    If you create and populate the Map in the thread that starts the other threads … before it does this … you should be fine.

    Another simple way to ensure safety is to do something like this:

    public class Foo {
        private final Map map;
    
        public Foo(....) {
            map = new HashMap();
            // populate
        }
    
        public Map getMap() {
            // If the 'map' is final, and nothing changes it apart from 
            // the constructor, this method doesn't need to be synchronized.
        }
    

    Both of these will result in a “happens-before” relationship between the creation / initialization, and each thread’s first use of the Map.

    You could also declare map in the above to be volatile, but is likely to cause more cache flushes than strictly need to occur; i.e. you’ll take a small performance hit.

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

Sidebar

Related Questions

I want to pass parameters from PHP Command Line Interface, and then read in
My data is just number, I want pass it from VC1 to VC2 for
I want to pass an ImageIcon instance by value as we know that by
I want to pass a string that contains few words to jQuery when click
I want to pass an uninitialized object pointer to some method. Within that method
I'm traversing an object graph and want to pass it a block that will
I want to pass data from WebApplication 1 to WebApplication 2. WebApplication 1 contains
I want pass a class-instace to a method of an other and be sure
I want to pass '#' to the query string like ?page.aspx?someParam=1234#5 .
I want to pass these parameters with a HTTP request to a WebApp hosted

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.