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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:00:49+00:00 2026-06-09T01:00:49+00:00

Consider a volatile int sharedVar . We know that the JLS gives us the

  • 0

Consider a volatile int sharedVar. We know that the JLS gives us the following guarantees:

  1. every action of a writing thread w preceding its write of value i to sharedVar in program order happens-before the write action;
  2. the write of value i by w happens-before the successful read of i from sharedVar by a reading thread r;
  3. the successful read of i from sharedVar by the reading thread r happens-before all subsequent actions of r in program order.

However, there is still no wall-clock time guarantee given as to when the reading thread will observe the value i. An implementation that simply never lets the reading thread see that value still complies with this contract.

I have thought about this for a while and I can’t see any loopholes, but I assume there must be. Please, point out the loophole in my reasoning.

  • 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-09T01:00:50+00:00Added an answer on June 9, 2026 at 1:00 am

    Turns out that the answers and the ensuing discussions only consolidated my original reasoning. I now have something in the way of a proof:

    1. take the case where the reading thread executes in full before the writing thread starts executing;
    2. note the synchronization order that this particular run created;
    3. now shift the threads in wall-clock time so they execute in parallel, but maintain the same synchronization order.

    Since the Java Memory Model makes no reference to wall-clock time, there will be no obstructions to this. You now have two threads executing in parallel with the reading thread observing no actions done by the writing thread. QED.

    Example 1: One writing, one reading thread

    To make this finding maximally poignant and real, consider the following program:

    static volatile int sharedVar;
    
    public static void main(String[] args) throws Exception {
      final long startTime = System.currentTimeMillis();
      final long[] aTimes = new long[5], bTimes = new long[5];
      final Thread
        a = new Thread() { public void run() {
          for (int i = 0; i < 5; i++) {
            sharedVar = 1;
            aTimes[i] = System.currentTimeMillis()-startTime;
            briefPause();
          }
        }},
        b = new Thread() { public void run() {
          for (int i = 0; i < 5; i++) {
            bTimes[i] = sharedVar == 0?
                System.currentTimeMillis()-startTime : -1;
            briefPause();
          }
        }};
      a.start(); b.start();
      a.join(); b.join();
      System.out.println("Thread A wrote 1 at: " + Arrays.toString(aTimes));
      System.out.println("Thread B read 0 at: " + Arrays.toString(bTimes));
    }
    static void briefPause() {
      try { Thread.sleep(3); }
      catch (InterruptedException e) {throw new RuntimeException(e);}
    }
    

    As far as JLS is concerned, this is a legal output:

    Thread A wrote 1 at: [0, 2, 5, 7, 9]
    Thread B read 0 at: [0, 2, 5, 7, 9]
    

    Note that I don’t rely on any malfunctioning reports by currentTimeMillis. The times reported are real. The implementation did choose, however, to make all actions of the writing thread visible only after all the actions of the reading thread.

    Example 2: Two threads both reading and writing

    Now @StephenC argues, and many would agree with him, that happens-before, even though not explicitly mentioning it, still implies a time ordering. Therefore I present my second program that demonstrates the exact extent to which this may be so.

    public static void main(String[] args) throws Exception {
      final long startTime = System.currentTimeMillis();
      final long[] aTimes = new long[5], bTimes = new long[5];
      final int[] aVals = new int[5], bVals = new int[5];
      final Thread
        a = new Thread() { public void run() {
          for (int i = 0; i < 5; i++) {
            aVals[i] = sharedVar++;
            aTimes[i] = System.currentTimeMillis()-startTime;
            briefPause();
          }
        }},
        b = new Thread() { public void run() {
          for (int i = 0; i < 5; i++) {
            bVals[i] = sharedVar++;
            bTimes[i] = System.currentTimeMillis()-startTime;
            briefPause();
          }
        }};
      a.start(); b.start();
      a.join(); b.join();
      System.out.format("Thread A read %s at %s\n",
          Arrays.toString(aVals), Arrays.toString(aTimes));
      System.out.format("Thread B read %s at %s\n",
          Arrays.toString(bVals), Arrays.toString(bTimes));
    }
    

    Just to help understanding the code, this would be a typical, real-world result:

    Thread A read [0, 2, 3, 6, 8] at [1, 4, 8, 11, 14]
    Thread B read [1, 2, 4, 5, 7] at [1, 4, 8, 11, 14]
    

    On the other hand, you’d never expect to see anything like this, but it is still legit by the standards of the JMM:

    Thread A read [0, 1, 2, 3, 4] at [1, 4, 8, 11, 14]
    Thread B read [5, 6, 7, 8, 9] at [1, 4, 8, 11, 14]
    

    The JVM would actually have to predict what Thread A will write at time 14 in order to know what to let the Thread B read at time 1. The plausibility and even feasibility of this is quite dubious.

    From this we can define the following, realistic liberty that a JVM implementation can take:

    The visibility of any uninterrupted sequence of release actions by a thread can be safely postponed until before the acquire action that interrupts it.

    The terms release and acquire are defined in JLS §17.4.4.

    A corrollary to this rule is that the actions of a thread which only writes and never reads anything can be postponed indefinitely without violating the happens-before relationship.

    Clearing up the volatile concept

    The volatile modifier is actually about two distinct concepts:

    1. The hard guarantee that actions on it will respect the happens-before ordering;
    2. The soft promise of a runtime’s best effort towards a timely publishing of writes.

    Note the point 2. is not specified by the JLS in any way, it just kind of arises by general expectation. An implementation that breaks the promise is still compliant, obviously. With time, as we move to massively parallel architectures, that promise may indeed prove to be quite flexible. Therefore I expect that in the future the conflation of the guarantee with the promise will prove to be insufficient: depending on requirement, we’ll need one without the other, one with a different flavor of the other, or any number of other combinations.

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

Sidebar

Related Questions

Consider the following code: int main() { int i; volatile int* p = &i;
Consider the following: volatile uint32_t i; How do I know if gcc did or
Consider the following Java code: volatile boolean v1 = false; volatile boolean v2 =
Consider the following C#: // C# .net switch(x) { case 1: for(int i =
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Consider the following code, which takes place in a background thread (thread B): List<T>
Consider this C code: extern volatile int hardware_reg; void f(const void *src, size_t len)
Consider the two following programs: program one int main() { printf( hello\n ); }
Consider the following x86 code example: #include <stdlib.h> static int i; static inline __attribute__((always_inline))

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.