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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:09:07+00:00 2026-05-23T15:09:07+00:00

I have a question regarding the Java Memory Model. Here is a simple class

  • 0

I have a question regarding the Java Memory Model. Here is a simple class presenting the problem:

public class ImmutableIntArray {

    private final int[] array;

    public ImmutableIntArray() {
        array = new int[10];
        for (int i = 0; i < 10; i++) {
            array[i] = i;
        }
    }

    // Will always return the correct value?
    public int get(int index) {
        return array[index];
    }

}

As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that other threads will see the most recent version of data stored in the array after construction.

Of course the code above is just a simple example presenting the problem, actually I want to implement a simple cache for direct byte buffers and I wouldn’t like to rely on some Collection classes. Currently I am using a ReentrantReadWriteLock to ensure the correct behaviour but I would like avoid it if it is possible.

  • 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-23T15:09:07+00:00Added an answer on May 23, 2026 at 3:09 pm

    In this example, everything will be fine (hmm, let’s suspend judgement a bit). Immutability is ambrosia when it comes to thread-safety – if a value cannot change, the majority of concurrency problems are immediately no longer a concern.

    Amir mentioned volatile which is generally useful – but the constructor also has similar semantics for final variables that ensure visibility. See JLS clause 17.5 for details – essentially the constructor forms a happens-before relationship between the write to the final variables and any subsequent reads.

    EDIT: So you set the values reference to the array in the constructor, it’s visible across all threads at that point, and then it doesn’t change. So we know all other threads will see the same array. But what about the array’s contents?

    As it stands, array elements don’t have any special semantics with regard to volatility, they’re as if you just declared a class yourself something like:

    public class ArrayTen {
        private int _0;
        private int _1;
        // ...
        private int _9;
    
        public int get(int index) {
           if (index == 0) return _0;
           // etc.
        }
    }
    

    So – another thread will only see these variables if we can do something to establish the happens-before relationship. And if my understanding is correct this requires but a small change to your original code.

    We already know that the setting of the array reference happens-before the end of the constructor. An additional point which is always true, is that actions in one thread happen-before later actions in that same thread. So we can combine these by setting the array fields first, and then assigning the final field, so as to get this transitive guarantee of visibility. This will of course require a temporary variable:

    public class ImmutableIntArray {
    
        private final int[] array;
    
        public ImmutableIntArray() {
            int[] tmp = new int[10];
            for (int i = 0; i < 10; i++) {
                tmp[i] = i;
            }
            array = tmp;
        }
    
        // get() etc.
    }
    

    I think this is guaranteed to be safe, now that we’ve switched the seemingly irrelevant order of assignment and population.

    But again, there might be something else I’ve missed which means the concurrency guarantees aren’t as robust as hoped. This question is to my mind an excellent example of why writing bulletproof multithreaded code is tricky, even when you think you’re doing something very simple, and how it takes a lot of thought and caution (and then bugfixes) to get right.

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

Sidebar

Related Questions

I have a simple question regarding java generics. How do I construct a generic
I have a question regarding how to use JSON integration in Android. For example
I have a formatting question for my Java swing application. It should be fairly
I have a java project written using eclipse ide and I want to run
I have a weird issue regarding buffering inputStream of pdf and odt files. They
I have some questions regarding ScalaTest: How can I ensure test-execution ORDER with ScalaTest,
I am in the process of starting to write a Java library to implement
I have the following two implementation of reading csv files, the csv files in
Some preface: I'm a computer engineering student taking a first class in C after
I'm writing a web application in Symfony for the first time, so I have

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.