Volatile variables for Java concurrency programming

Source: Internet
Author: User

Volatile provides a weak synchronization mechanism to ensure that variable updates are notified to other threads. Volatile variables are not cached in registers or invisible to other processors, so the most recent write value is always returned when reading a volatile variable. You can imagine the following semantics, but volatile is a more lightweight synchronization mechanism.volatile can only ensure visibility, but it does not guarantee atomicity. In other words, it cannot be used in composite operationsvolatile variables, such as i++.
  
 

    public   void ( >int

    this.value = value;

    }

    public synchronized int getValue(){

    return value;

    }

Volatile variables can be used when all of the following conditions are true
    • The write operation on a variable does not depend on the current value of the variable, or you can ensure that only a single thread updates the value of the variable.

    • The variable does not incorporate the invariant condition with other state variables

    • The access variable is not required to lock



From for notes (Wiz)

Volatile variables for Java concurrency programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.