One, visibility what is visibility? Java thread safety needs to prevent a thread from using object state while another thread changes the state at the same time, and it needs to ensure that when a thread changes the state of the object. Other threads can see the state changes that have occurred. The latter is the descriptive narrative of the visibility that multithreading is able to get the status of other threads in real time. Two workers to be added at the same time to record the number of production products
1. Failure data visibility failure is the state in which other threads have not acquired the change. A more intuitive narrative is that the data obtained by other threads is invalid data.
2. Non-atomic 64-bit operation
3. Locking and visibility For example, when a variable is read with a lock on the +1, the lock guarantees that the other thread gets the value after the variable is +1, so it can guarantee visibility.
4. The volatile variable thread changes the shared variable. Visible to other threadsto meet the following conditions, use Volatitle:A 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 a variable. The variable is not included in the non-deformation condition along with other state variables. There is no need to lock when visiting variables.
Publish or escape the disclosure of an object means. Enables objects to be used in code outside the scope of the current period. A simple way to understand the object that gets to the current class elsewhere is to advertise the current class. Java multithreading not only ensures that the current class is thread-safe, but also guarantees thread safety in all areas where the current class object is used.
III. Encapsulation Thread closure-the shared data is used only in threads, not shared. For example, Java's threadlocal class Ad-hoc thread closure-the responsibility for maintaining thread closeness is entirely the responsibility of the program implementation. (very fragile) thread closure-a particular subsystem is typically implemented as a special case of a single-threaded subsystem stack closed thread closure, such as a primitive type of local variable threadlocal-usually prevents a mutable single-instance object or global variable from being shared.
The immutable final object cannot be changed by itself, but the variables in final can change
Five, the safe publication Way: 1. Initializes an object reference 2 in a static initialization function. Saves the object's reference to a volatile type of field or Atomicreference object 3. Save the object's reference to the final type field of the correct constructed object 4. Save a reference to an object in a lock-protected domain
Java concurrency Programming (ii): Sharing of objects
Explore concurrent Programming (iii)------Java storage model and shared objects
Synchronization and Java memory Model (iii) visibility
Java concurrent programming in the third chapter of the object sharing reading notes