1. Program order rules: the same thread, according to the code order, to be precise, according to the control flow order rather than the program code order
2. Pipeline locking rules: the unlock operation precedes the subsequent lock operation for the same lock
3. volatile variable rules: For modified variables, the write operation here occurs first before the read operation.
4. Thread start rules: The start method of the Thread object precedes every action that occurs in this thread.
5. Thread termination rules: All operations of a thread occur first in the termination detection of this thread.
6. Thread interruption rules: The call to the thread interrupt() method occurs first when the code of the interrupted thread detects the occurrence of the interruption time.
7. Object finalization rules: A thread initialization completes prior to the beginning of its finalize() method.
8. Transitivity: Antecedent is transitive.
The following is an example to illustrate the principle relationship of thread occurrence
package cn.edu.hust; public class Node { private int value; private int leftChild=Integer.MAX_VALUE; private int rightChild=Integer.MAX_VALUE; public Node() { } public Node(int value, int leftChild, int rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public boolean isLeaf() { return leftChild==Integer.MAX_VALUE&&rightChild==Integer.MAX_VALUE; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getLeftChild() { return leftChild; } public void setLeftChild(int leftChild) { this.leftChild = leftChild; } public int getRightChild() { return rightChild; } public void setRightChild(int rightChild) { this.rightChild = rightChild; } }
If the above getter and setter methods are in different threads, because the above rules are not suitable, then it is thread-unsafe.