Home java Java volatile keyword

Java volatile keyword

Author

Date

Category

I met this code today

class someClass {
 // ...
 private volatile int a;
 // ...
}

Answer 1, authority 100%

The volatile modifier imposes some additional conditions on the read / write of a variable. It is important to understand two things about volatile variables:

  1. Read / write operations on a volatile variable are atomic.
  2. The result of an operation of writing a value to a volatile variable by one thread becomes visible to all other threads that use this variable to read a value from it.

Answer 2, authority 61%

This means that the value of the variable will be “always read”. For example, in multithreaded applications, one thread read the value a = 1, passed control to another thread, which changed the value to a = 2, then control returned. So, without volatile the value of a for the first thread will be 1, since the first thread “remembers” that a = 1, with volatile – 2, because the first thread will read the value again and get the already changed one.


Answer 3, authority 39%

  • the variable has a master copy plus a copy for each thread,
    who is using it. The master copy is syncronized with the local
    a copy of the thread when entering / leaving the synchronized block.
    Sometimes, for example, an empty synchronized (lock) {} block makes sense.

  • variables with the volatile modifier have no local copies.
    All threads work with master copy.


  • Answer 4, authority 23%

    This is the definition given in the article “Java Multithreading” on the site http://alfalavista.ru .

    Defining a variable with the volatile keyword means that
    the value of this variable can be changed by other threads. To
    to understand what volatile does, it’s helpful to understand how threads
    process regular variables.

    For performance reasons, the Java language specification allows
    storing a local copy of the variable in the JRE for each thread,
    which links to it. Such “local” copies of variables
    resemble a cache and help a thread avoid accessing main memory
    every time you want to get the value of a variable. At startup
    two threads, one of them reads the variable A as 5, and the second as
    10. If the value of variable A has changed from 5 to 10, then the first thread will not know about the change and will store the wrong value A. But
    if variable A is marked as volatile , then whenever the stream reads
    value of A, it will access the master copy of A and read it
    present value. Stream local cache makes sense if
    variables in your applications will not be changed externally.

    If a variable is declared volatile , it means that it can be changed by different threads. It is natural to expect the JRE to provide some form of synchronization for such volatile variables. The JRE does implicitly provide synchronization when accessing volatile variables, but with one very big caveat: reading a volatile variable and writing to a volatile variable are synchronized, but non-atomic operations are not.


    Answer 5, authority 6%

    is optional for volatile object references. am i right?

    For example, when we use the Singleton pattern in a multithreaded application in which we use synchronization and want synchronization to occur only once when initializing an object, and not every time we call getInstance (), then we use the volatile modifier for the object reference:

    public class Singleton {
      private static volatile Singleton instance;
      private Singleton () {
      }
      public static Singleton getInstance () {
        if (instance == null) {
          synchronized (Singleton.class) {
            if (instance == null)
              instance = new Singleton ();
          }
        }
        return instance;
      }
    }
    

    Answer 6, authority 6%

    volatile – literally means volatile , volatile , volatile

    in the context of programming, this means that the value of a variable may change unexpectedly, so you should not rely on the values ​​of this variable, for example, if the code says:

    private volatile int i;
    // over time
    i = 0;
    while (i & lt; 10) {
     // blah-blah
     i ++;
    }
    

    this does not mean that the cycle will definitely end in 10 steps …

    It may well happen that during the loop, the volatile value of a variable will (or may not) change unexpectedly …


    Answer 7, authority 5%

    volatile – tells the thread that the variable can change, and informs the thread to refer to the latest version, not a hashed copy, and propagate changes in a timely manner.

    A “volatile” data member informs a thread, both to get the latest value for the variable (instead of using a cached copy) and to write all updates to the variable as they occur.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions