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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:13:58+00:00 2026-06-15T00:13:58+00:00

From docs: Using volatile variables reduces the risk of memory consistency errors But this

  • 0

From docs:

Using volatile variables reduces the risk of memory consistency errors

But this means that sometimes volatile variables don’t work correct?
Strange how it can be used – for my opinion it is very bad code that sometimes work sometimes not. I tried to Google but didn’t find example memory consistency error with volatile. Could you please propose one?

  • 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-15T00:13:59+00:00Added an answer on June 15, 2026 at 12:13 am

    The issue is not so much that volatile works unreliably. It always works the way it is supposed to work. The problem is that the way it is supposed to work is sometimes not adequate for concurrency control. If you use volatile in the wrong situation, you can still get memory consistency errors.

    A volatile variable will always have any writes propagated to all threads. However, suppose you need to increment the variable among various threads. Doing this(*):

    volatile int mCounter;
    
    // later, in some code that might be executed simultaneously on multiple threads:
    mCounter++;
    

    There is a chance that counter increments will be missed. This is because the value of mCounter needs to be first read by each thread before a new value can be written. In between those two steps, another thread may have changed the value of mCounter. In situations like this, you would need to rely on synchronized blocks rather than volatile to ensure data integrity.

    For more info on volatile vs. synchronized, I recommend the article Managing volatility by Brian Goetz

    (*) I realize that the above would be better implemented with AtomicInteger; it’s a contrived example to illustrate a point.

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

Sidebar

Related Questions

Using this example from http://docs.python.org/tutorial/classes.html#class-objects : class MyClass: A simple example class i =
I'm accessing the Google Contacts API using OAuth. I see from the docs that
I'm trying out http requests to download a pdf file from google docs using
I'm attempting to get order data from a Yahoo store. I'm using the docs
From: http://developers.soundcloud.com/docs/api/tracks#filtering I'm not sure from the docs if this functionality is available. I
This piece of code is from Adobe docs : package { import flash.display.Loader; import
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
From the docs: You usually access to-many relationships using mutableSetValueForKey:, which returns a proxy
I downloaded java docs or what i think was java docs from oracle. But
Trying to download docs from the Google Docs using Java client library and getting

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.