Java Multithreading--volatile keywords, publishing, and escaping

Source: Internet
Author: User

1. Volatile keyword 

The Java language provides a slightly weaker synchronization mechanism, the volatile variable. Variables modified by the volatile keyword are not cached in registers or in places that are not visible to other processors, so the most recent value is always returned each time the Volatitle type variable is read, rather than being fetched from the register.

  Since the locking mechanism ensures visibility and ensures atomicity, volatile can only ensure visibility.

2. Release and Escape

  " Publish (Publish)" an object that enables an object to be used in code outside the current scope. If you save a reference to the object to a place that other code can access, either return the reference in a non-private method, or pass the reference to a method in another class.

This is called escaping (escape) when an object that should not be published is published.

Java Multithreading--volatile keywords, publishing, and escaping

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.