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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:12:34+00:00 2026-05-15T21:12:34+00:00

Suppose I have a instance variable that has original value: Integer mMyInt = 1;

  • 0

Suppose I have a instance variable that has original value:

Integer mMyInt = 1;

There are two threads.

The first changes mMyInt by calling:

void setInt() {
    mMyInt = 2;
}

The second thread gets mMyInt by calling:

Integer getInt() {
  return mMyInt;
}

Both threads don’t use synchronization.

My questions is, what is the possible value the the second thread can get from getInt()? Can it be only 1 or 2? Can it get null?

Thanks

  • 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-15T21:12:34+00:00Added an answer on May 15, 2026 at 9:12 pm

    EDIT: Important update thanks to @irreputable.

    Unless the object has escaped during construction (see below), the assignment mMyInt=1 happens before any access to the getter/setter. Also in java, object assignment is atomic (there is 0 chance that you observe some invalid address assigned. Be careful because 64bit primitive assignments, such as double and long are NOT atomic).

    So, in that case the possible value is either 1 or 2.

    Object can escape during construction in this kind of situation:

     class Escape {
        Integer mmyInt = 1;
    
        Escape(){
            new Thread(){
                public void run(){
                    System.out.println(Escape.this.mmyInt);
                }
            }.start();
        }
     }
    

    Although in practice it probably rarely happens, in the above case, the new thread can observe an not fully constructed Escape object and thus in theory get an mmyInt value of null (AFAIK you still won’t get some random memory location).

    What if it it is a HashMap
    object? The instance variable mMyMap
    has original value. Then, the first
    thread calls “mMyMap = new HashMap();”
    The second thread calls “return
    mMyMap;” Can the second thread get
    null, or can it only get original or
    new HashMap object?

    When “Object reference assignment is atomic”, it means that you will NOT observe an intermediate assignment. It’s either the value before, or the value after. So if the only assignment that is happening is map = someNonNullMap(); after the construction has completed (and the field was assigned a non null value during the construction) and the object has not escaped during the construction, you can’t observe null.

    Update:
    I consulted a concurrency expert, and according to him, the Java Memory Model allows compilers to reorder assignment and object construction (while in practice I imagine that would be highly unlikely).

    So for example in the below case, thread1 can allocate some heap, assign some value to map, the continue construction of map. Meanwhile thread2 comes and observe an partially constructed object.

    class Clever {
       Map map;
    
       Map getMap(){
           if(map==null){
               map = deriveMap();        }
           return map;
       }
    }
    

    JDK has a similar construct in the String class (not exact quote):

    class String {
       int hashCode = 0;
    
       public int hashCode(){
           if(hashCode==0){
               hashCode = deriveHashCode();
        }
           return hashCode;
       }
    }
    

    This DOES work because the non-volatile cache is primitive and not an object, according to the same concurrency experts.

    These problems can be avoided by introducing an happens before relationship. In the cases above, one could do this by declaring the members volatile. Also for 64bit primitive, declaring them volatile will make their assignment atomic.

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

Sidebar

Ask A Question

Stats

  • Questions 482k
  • Answers 482k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Select *, (((9/rateup+ratedown)*rateup)+1) As Ord from Table WHERE published =… May 16, 2026 at 6:50 am
  • Editorial Team
    Editorial Team added an answer You're getting rubbish printed out because recv does not null-terminate… May 16, 2026 at 6:50 am
  • Editorial Team
    Editorial Team added an answer You forgot http:// at the beginning of your Ajax url… May 16, 2026 at 6:50 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.