java-Multithreading in Depth (iii) Happens-before INTRODUCTION

Source: Internet
Author: User

There are visibility issues in multi-threading, see java-Multithreading in-depth (ii) Mutex and visibility, visibility followed by happens-before rules.

(i) Happens-before rules

Happens-before rule: If a happens-before B, then the impact of operation A can be known by B, the impact includes modifying the main memory shared variable values, sending messages, calling methods and so on.

80% Happens-before rules in the Java memory model:

1, program Order rules: single-threaded, according to the program code, the previous operation Happens-before after the operation.

2, locking rules: For the same lock, unlock operation Happens-before lock operation.

3. Volatile variable rule: Happens-before read operation for volatile variable write operation.

4, Transitivity: If the operation of a happens-before operation B, Operation b happens-before Operation C, then a happens-before operation C can be obtained.
5. Thread Start rule: The Start method of the thread object Happens-before every action of this thread.
6, thread termination rule: All operations of the thread happens-before the termination detection of this thread, it is possible to detect that the thread has terminated execution by means of the Thread.Join () method end, the return value of thread.isalive ().
7. Thread break rule: The call to Thread interrupt method Happens-before occurs when the code of the interrupted thread detects an interrupt.
8. Object Termination rule: Initialization of an object (completion of constructor execution) Happens-before the beginning of its Finalize method.


(ii) The relationship between TB and HB

TB (Time-before) and HB (Happens-before) do not have a direct relationship. TB refers to the operation of the previous TB operation in accordance with program execution.

1, TB does not mean HB

/** * Multithreaded Visibility Test *  * @author Peter_wang * @create-time 2015-1-12 pm 3:56:29 */public class Threadvisabledemo {    privat e static int a = 0;    Static class Getnumthread        extends Thread {        @Override public        void Run () {            System.out.println (a);//b1        }    }    Static class Changenumthread        extends Thread {        @Override public        void Run () {            a = 1;//a1,a2        }< c15/>}    /**     * @param args     *    /public static void main (string[] args) {        getnumthread Getnumthread = new Getnumthread ();        Changenumthread changenumthread = new Changenumthread ();        Changenumthread.start ();//c1        Getnumthread.start ();//c2    }}
C1 prior to C2 execution, there is C1 TB C2 relationship, but from the previous blog, a no visibility, execution A1 data will not necessarily be refreshed in time, B1 the final output can be 0 or 1,c1 HB C2 does not exist.

2.HB does not represent TB

A = 1;  //a1b = 2;   A2
according to the rules 1,a1 HB A2, because there is no data association between the two pieces of code (A1 and A2 interchange will not affect the results of the operation), the JVM automatically optimizes performance, there may be a compilation code reordering or execution instruction code reordering problem, so there may be A2 TB A1.


java-Multithreading in Depth (iii) Happens-before INTRODUCTION

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.