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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:11:03+00:00 2026-06-06T17:11:03+00:00

I could find the answer if I read a complete chapter/book about multithreading, but

  • 0

I could find the answer if I read a complete chapter/book about multithreading, but I’d like a quicker answer. (I know this stackoverflow question is similar, but not sufficiently.)

Assume there is this class:

public class TestClass {
   private int someValue;

   public int getSomeValue() { return someValue; }
   public void setSomeValue(int value) {  someValue = value; }
}

There are two threads (A and B) that access the instance of this class. Consider the following sequence:

  1. A: getSomeValue()
  2. B: setSomeValue()
  3. A: getSomeValue()

If I’m right, someValue must be volatile, otherwise the 3rd step might not return the up-to-date value (because A may have a cached value). Is this correct?

Second scenario:

  1. B: setSomeValue()
  2. A: getSomeValue()

In this case, A will always get the correct value, because this is its first access so he can’t have a cached value yet. Is this right?

If a class is accessed only in the second way, there is no need for volatile/synchronization, or is it?

Note that this example was simplified, and actually I’m wondering about particular member variables and methods in a complex class, and not about whole classes (i.e. which variables should be volatile or have synced access). The main point is: if more threads access certain data, is synchronized access needed by all means, or does it depend on the way (e.g. order) they access it?


After reading the comments, I try to present the source of my confusion with another example:

  1. From UI thread: threadA.start()
  2. threadA calls getSomeValue(), and informs the UI thread
  3. UI thread gets the message (in its message queue), so it calls: threadB.start()
  4. threadB calls setSomeValue(), and informs the UI thread
  5. UI thread gets the message, and informs threadA (in some way, e.g. message queue)
  6. threadA calls getSomeValue()

This is a totally synchronized structure, but why does this imply that threadA will get the most up-to-date value in step 6? (if someValue is not volatile, or not put into a monitor when accessed from anywhere)

  • 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-06T17:11:06+00:00Added an answer on June 6, 2026 at 5:11 pm

    The issue is that java is simply a specification. There are many JVM implementations and examples of physical operating environments. On any given combination an an action may be safe or unsafe. For instance On single processor systems the volatile keyword in your example is probably completely unnecessary. Since the writers of the memory and language specifications can’t reasonably account for possible sets of operating conditions, they choose to white-list certain patterns that are guaranteed to work on all compliant implementations. Adhering to to these guidelines ensures both that your code will work on your target system and that it will be reasonably portable.

    In this case “caching” typically refers to activity that is going on at the hardware level. There are certain events that occur in java that cause cores on a multi processor systems to “Synchronize” their caches. Accesses to volatile variables are an example of this, synchronized blocks are another. Imagine a scenario where these two threads X and Y are scheduled to run on different processors.

    X starts and is scheduled on proc 1
    y starts and is scheduled on proc 2
    
    .. now you have two threads executing simultaneously
    to speed things up the processors check local caches
    before going to main memory because its expensive.
    
    x calls setSomeValue('x-value') //assuming proc 1's cache is empty the cache is set
                                    //this value is dropped on the bus to be flushed
                                    //to main memory
                                    //now all get's will retrieve from cache instead
                                    //of engaging the memory bus to go to main memory 
    y calls setSomeValue('y-value') //same thing happens for proc 2
    
    //Now in this situation depending on to order in which things are scheduled and
    //what thread you are calling from calls to getSomeValue() may return 'x-value' or
    //'y-value. The results are completely unpredictable.  
    

    The point is that volatile(on compliant implementations) ensures that ordered writes will always be flushed to main memory and that other processor’s caches will be flagged as ‘dirty’ before the next access regardless of the thread from which that access occurs.

    disclaimer: volatile DOES NOT LOCK. This is important especially in the following case:

    volatile int counter;
    
    public incrementSomeValue(){
        counter++; // Bad thread juju - this is at least three instructions 
                   // read - increment - write             
                   // there is no guarantee that this operation is atomic
    }
    

    this could be relevant to your question if your intent is that setSomeValue must always be called before getSomeValue

    If the intent is that getSomeValue() must always reflect the most recent call to setSomeValue() then this is a good place for the use of the volatile keyword. Just remember that without it there is no guarantee that getSomeValue() will reflect to most recent call to setSomeValue() even if setSomeValue() was scheduled first.

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

Sidebar

Related Questions

I've read every answer available for this question that I could find, including this
I've read a few git questions here, but could not find an answer to
First, I know this is kind of common question, but I could not find
This may be a basic question, but I could not find an answer for
i tried to find some information on this but could not find any answer.
This is probably a very novice question, but I could not find an answer.
I reviewed similar questions but could not find an answer to my specific quandry.
I have been searching everywhere but I could not find an answer. I need
Yes, I've googled and I could not find the answer to this problem. First
I could not find a clear answer to this question elsewhere, so I'll try

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.