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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:32:03+00:00 2026-05-27T08:32:03+00:00

Reading here: JLS 8.3.1.4 volatile Fields Without volatile it says then method two could

  • 0

Reading here:

JLS 8.3.1.4 volatile Fields

Without volatile it says

“then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and”

class Test {
    static volatile int i = 0, j = 0;
    static void one() { i++; j++; }
    static void two() {
        System.out.println("i=" + i + " j=" + j);
    }
}

With volatile is says

“It is possible, however, that any given invocation of method two might observe a value for j that is much greater than the value observed for i, because method one might be executed many times between the moment when method two fetches the value of i and the moment when method two fetches the value of j.”

In behaves ‘properly’ with synchornization, but I’m confused as to what benefit volatile brings here?

I thought volatile gaurantees the order is preserved, so I would have thought it SOME cases the value of i might be greater than j, but not the other way around since that implies the order of incrementing was changed.

Is that a typo in the doc? If not, please explain how j could be greater than i when using volatile.

  • 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-27T08:32:04+00:00Added an answer on May 27, 2026 at 8:32 am

    It is saying that in the middle of method two method one could run several and that the value read for j would be higher than the value read for i.

    read i
    run method 1
    run method 1
    read j
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through some of the questions here, the general concensus seems to be that
I was reading here that synchronous ajax calls in jquery don't time out. Is
After reading a question here about what things our computer could do in one
I was reading here , and I noticed that, for example, if I have
reading the docs here:http://grails.org/plugin/jquery-ui it says to use a custom theme I should put
I was just reading here that I shouldn't throw ArgumentNullException from within my own
From reading here and around the net, I'm close to assuming the answer is
I need some mind reading here, since I am trying to do what I
I've been reading things here and there for a while now about using an
While I'm busy here reading the FastCGI documentation (yeah, reading the 'friendly' manual), does

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.