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

The Archive Base Latest Questions

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

I’ve been reading up about the use of volatile variables in Java. I understand

  • 0

I’ve been reading up about the use of volatile variables in Java. I understand that they ensure instant visibility of their latest updates to all the threads running in the system on different cores/processors. However no atomicity of the operations that caused these updates is ensured. I see the following literature being used frequently

A write to a volatile field happens-before every read of that same field .

This is where I am a little confused. Here’s a snippet of code which should help me better explain my query.

volatile int x = 0;
volatile int y = 0; 

Thread-0:                       |               Thread-1:
                                |
if (x==1) {                     |               if (y==1) {
     return false;              |                    return false; 
} else {                        |               } else {
     y=1;                       |                   x=1;
     return true;               |                   return true;
}                               |               }

Since x & y are both volatile, we have the following happens-before edges

  1. between the write of y in Thread-0 and read of y in Thread-1
  2. between the write of x in Thread-1 and read of x in Thread-0

Does this imply that, at any point of time, only one of the threads can be in its ‘else’ block(since a write would happen before the read)?

It may well be possible that Thread-0 starts, loads x, finds it value as 0 and right before it is about to write y in the else-block, there’s a context switch to Thread-1 which loads y finds it value as 0 and thus enters the else-block too. Does volatile guard against such context switches (seems very unlikely)?

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

    So I think this question is a bit in the weeds, the gist is that volatile indicates that the value of the variable may change outside the scope of the current thread, and that its value must always be read before use.

    In principle, the statement you’re quoting is really saying that prior to replacing the value with the current thread, the value will be read.

    Your example is a race condition, both threads may return true, neither may return true, or they may each return a different value — the semantics of volatile won’t define execution for your example (I’d encourage you to compile and run it and see that the output varies).

    A common way to illustrate the behavior of volatile is to run two threads, where one thread updates shared state and to see what happens when the field is marked, and when it isn’t:

    class VolatileTest implements Runnable
    {
            // try with and without volatile
            private volatile boolean stopRunning = false;
    
            public void triggerStop(){
                 stopRunning = true;
            }
    
            @Override
            public void run(){
                 while(!stopRunning);
                 System.out.println("Finished.");
            }
    
            public static void main (String[] args) throws java.lang.Exception
            {
                final VolatileTest test = new VolatileTest();
                new Thread(test).start();
                Thread.sleep(1000);
                test.triggerStop() = false;
            }
    }
    

    In this example, failure to mark stopRunning as volatile can lead to the while loop continuing forever, as unless stopRunning is marked as volatile it’s not required to read the value on each iteration.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.