Happens-before rules

Source: Internet
Author: User

Happens-before principle:
    • Happens-before It is the main basis for judging whether the data is competitive and whether the thread is safe or not. In order to ensure thread safety, we can make 2 operations have a happens-before relationship
    1. If one operation Happens-before with another, the result of the first operation is visible to the second operation, and the first operation is executed before the second operation, but this order is only for programmers, and actual execution is not necessarily.
    2. If the result of the 2 operation reordering is the same as the result of the Happens-before rule execution, the reordering is allowed.
Happens-before's 8 rules ensure that a single thread with Happens-before rules or 2 operations in multiple threads is safe.
    1. Program Order rules : A line range , in the code order, written in front of the operation first occurs in the operation after writing.
    2. Monitor Lock Rule : The unlocking of a lock is happens-before to the subsequent locking of the lock.
    3. volatile variable rule : writes to a volatile field, happens-before to any subsequent reading of this volatile field.
    4. transitivity : If a happens-before B, and B happens-before C, then a Happens-before c.
    5. start () Rule : If thread A performs an operation Threadb.start () (boot thread B), then the Threadb.start () operation of the A thread happens-before any action in thread B.
    6. join () Rule : If thread A performs an operation Threadb.join () and returns successfully, any action in thread B happens-before the successful return of thread A from the Threadb.join () operation
    7. thread Termination rule : All operations in a thread occur first in the thread termination detection, and we can detect that the thread has terminated execution by means of the Thread.Join () method end, Thread.isalive () return value.
    8. object Finalization Rule : Initialization of an object occurs at the beginning of his finalize () method;
Resources
    1. The art of Java concurrent programming
    2. "Happens-before java concurrency"-----java memory model

Happens-before rules

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.