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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:15:10+00:00 2026-05-31T23:15:10+00:00

From book Java Concurrency in Practice page 26: You can use volatile variables only

  • 0

From book “Java Concurrency in Practice” page 26:

You can use volatile variables only when all the following criteria are met:

  • Writes to the variable do not depend on its current value, or you can ensure that only a single thread ever updates the value;

  • The variabledoes not participate in invariants with other state variables; and 

  • Locking is not required for any other reason while the variable is being accessed.

How to comprehend “The variable does not participate in invariants with other state variables when using volatile keyword“?

  • 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-31T23:15:11+00:00Added an answer on May 31, 2026 at 11:15 pm

    A simple definition of “invariant”: a condition that is always true during the lifetime of an object.

    Volatile variables do not share the atomicity features of synchronized blocks.

    That’s why you can’t use them within a class that has invariants that relate multiple variables.

    For example imagine you have a class to model a time interval described by two variables: start and end. An invariant condition may be that start is always less or equal than end. If both variables (just as example) are declared as volatile then you can rely on visibility features of volatile but you can’t be sure that during a change that involves both variables the invariant is always satisfied. Think:

    public void setInterval(Date newStart, Date newEnd)
    {
     // Check if inputs are correct
    
     // Here the object state is valid
     start = newStart;
    
     // If another thread accesses this object now it will
     // see an invalid state because start could be greater than end
    
     end = newEnd;
     // Here the object state is valid again
    }
    

    In this case you can be sure that the change is visible to every thread but in the middle of the two instructions the object state could be not valid. Because it can be accessed by other threads (remember this is a simple case so it’s possible but not likely) then the invariant condition “start < end” could be broken.

    That’s why the use of volatile is somehow discouraged outside a (small) set of well defined patterns. A volatile variable should be used only if these conditions are satisfied:

    • The variable isn’t involved in invariants related to other variables (for the reason explained above).
    • The value to write on the variable does not depend on its current value.

    For example the expression int a = i++; isn’t atomic then it’s not – strictly speaking – thread-safe because it’ll be rewritten with something like this:

    int temp = i;
    i = i + 1;
    int a = temp;
    

    To make it atomic from a thread point of view you can imagine a class like this:

    public class MyAtomicInteger
    {
      public synchronized increment()
      {
        x = x + 1;
      }
    
      private int x;
    }
    

    Of course it exists a true implementation of this AtomicInteger and it’s part of the package java.util.concurrent.atomic, it provides some simple basic routines for lock-free concurrent programming.

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

Sidebar

Related Questions

Code from the book Java Concurrency in Practice Listing 8.1 Why is the code
I found the book Java Concurrency in Practice to be an excellent guide to
As I read from various Java book and tutorials, variables declared in a interface
From the book Groovy and Grails recipes I'm using the following code snippet: String
this is basic example I got for a GUI from a book called Java
I am doing some exercises from the book Thinking In Java . I have
I am reading Java Concurrency in Practice and kind of confused with the thread
A quote from the book I'm reading Head First Java : The point is
I am a beginner in Java and am doing an exercise from a book.
I have a doubt from the book Efective Java. The doubt is regarding equals

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.