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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:15:51+00:00 2026-06-12T16:15:51+00:00

Could someone validate my understanding of the memory fence established after a constructor executes.

  • 0

Could someone validate my understanding of the memory fence established after a constructor executes. For example, suppose I have a class called Stock.

public final class Stock{

       private final String ticker;
       private double qty;
       private double price;

       public Stock ( String ticker, double qty, double price ){
              this.ticker  = ticker;
              this.qty     = qty;
              this.price   = price;

              //I am assuming a memory fence gets inserted here.
       }


       public final void updateQty( double qty ){
           this.qty = qty;
       }


       public final void updatePrice( double price ){
           this.price = price;
       }

}

Further, assume that the constructor is executed by Thread1 and then updateQty() and updatePrice() are called several time by Thread2 (always by Thread2).

My contention is that after Thread1 creates the object, the object’s “visibility” is established with all the other threads in the jvm. And since the two mutable variables are only changed by Thread2, I don’t need any locking. Am I correct?

  • 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-12T16:15:53+00:00Added an answer on June 12, 2026 at 4:15 pm

    My contention is that after Thread1 creates the object, the object’s “visibility” is established with all the other threads in the jvm.

    This is not correct. There is no implied constructor memory barrier/fence which is why instruction reordering around the constructor is such a problem. If you are going to be using a Stock object in another thread other than the one that constructed it, you are going to have to synchronize on the object before calling any update methods.

    And since the two mutable variables are only changed by Thread2, I don’t need any locking.

    After you initially synchronize on the object in Thread2 you would not need any additional locking unless you wanted to see those mutated fields in other threads. If multiple threads are reading from your Stock object while Thread2 is mutating it then all threads need to cross a memory barrier either through synchronization or by making the fields mutated fields volatile.

    This has to do both with constructor operation reordering and memory visibility. See this answer for more of the pitfalls around constructor reordering:

    Is this a safe publication of object?

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

Sidebar

Related Questions

I want to validate my value, but doesnt quite work if someone could help
I'd really appreciate it if someone could validate my SQL query. For the following
I was hoping someone could validate my assumptions before I recommend that my client
Could someone know where is saved path to external XML-File that contain configuration for
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please suggest why this is happening... I’ve got some code to pretty
Could someone help me to add A-atype B-btype C-ctype ONLY as a legend to
Could someone provide a sample implementation for rotating a button on a thread ?
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
could someone please tell me what I need to do in order to create

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.